\ingroup annotations Annotation for arguments with values
| 1936 | /// \ingroup annotations |
| 1937 | /// Annotation for arguments with values |
| 1938 | struct arg_v : arg { |
| 1939 | private: |
| 1940 | template <typename T> |
| 1941 | arg_v(arg &&base, T &&x, const char *descr = nullptr) |
| 1942 | : arg(base), value(reinterpret_steal<object>(detail::make_caster<T>::cast( |
| 1943 | std::forward<T>(x), return_value_policy::automatic, {}))), |
| 1944 | descr(descr) |
| 1945 | #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) |
| 1946 | , |
| 1947 | type(type_id<T>()) |
| 1948 | #endif |
| 1949 | { |
| 1950 | // Workaround! See: |
| 1951 | // https://github.com/pybind/pybind11/issues/2336 |
| 1952 | // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700 |
| 1953 | if (PyErr_Occurred()) { |
| 1954 | PyErr_Clear(); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | public: |
| 1959 | /// Direct construction with name, default, and description |
| 1960 | template <typename T> |
| 1961 | arg_v(const char *name, T &&x, const char *descr = nullptr) |
| 1962 | : arg_v(arg(name), std::forward<T>(x), descr) {} |
| 1963 | |
| 1964 | /// Called internally when invoking `py::arg("a") = value` |
| 1965 | template <typename T> |
| 1966 | arg_v(const arg &base, T &&x, const char *descr = nullptr) |
| 1967 | : arg_v(arg(base), std::forward<T>(x), descr) {} |
| 1968 | |
| 1969 | /// Same as `arg::noconvert()`, but returns *this as arg_v&, not arg& |
| 1970 | arg_v &noconvert(bool flag = true) { |
| 1971 | arg::noconvert(flag); |
| 1972 | return *this; |
| 1973 | } |
| 1974 | |
| 1975 | /// Same as `arg::nonone()`, but returns *this as arg_v&, not arg& |
| 1976 | arg_v &none(bool flag = true) { |
| 1977 | arg::none(flag); |
| 1978 | return *this; |
| 1979 | } |
| 1980 | |
| 1981 | /// The default value |
| 1982 | object value; |
| 1983 | /// The (optional) description of the default value |
| 1984 | const char *descr; |
| 1985 | #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) |
| 1986 | /// The C++ type name of the default value (only available when compiled in debug mode) |
| 1987 | std::string type; |
| 1988 | #endif |
| 1989 | }; |
| 1990 | |
| 1991 | /// \ingroup annotations |
| 1992 | /// Annotation indicating that all following arguments are keyword-only; the is the equivalent of |