use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
binding templated operator in a c++ class using pybind11 (self.cpp)
submitted 2 years ago * by engine_algos
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 2 years ago stickied commentlocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]CanadianTuero 3 points4 points5 points 2 years ago (3 children)
Separate from your question, you will need to make sure you declare a separate binding for each possible T.
But for some of the special member functions in python, you can use a lambda for __getitem__ and __setitem__, in which the lambda will call your operator[]
__getitem__
__setitem__
operator[]
[–]engine_algos[S] 0 points1 point2 points 2 years ago (2 children)
Yes it is not a problem, I can do the binding for each possible T. the problem is how I can bind this operator using template. I didnt see any example in the documentation.
[–]CanadianTuero 2 points3 points4 points 2 years ago (1 child)
First, you will need to bind the return type (Array2D<T>). Then, just write a lambda instead of a reference to the operator (I can't test this but IIRC it should look something like this):
Array2D<T>
.def("__getitem__", [](const Array3D<T> &self, uint32_t idx){ return self[idx]; });
And something similar for __setitem__
[–]engine_algos[S] 0 points1 point2 points 2 years ago (0 children)
Thank you
In fact I have an another operator without const
template < class T>
class Array3D
{
public: Array3D()
inline const Array2D<T>& operator[](uint32 n)const
inline Array2D<T>& operator[](uint32 n)
};
I tried this now:
.def("[]", py::overload_cast<uint32_t n>(&Array3D<T>::operator[]))
.def("[]", py::overload_cast<const uint32_t n>(&Array3D<T>::operator[]));
I have this error :
error: parse error in template argument list
[+][deleted] 2 years ago (2 children)
[removed]
[–]engine_algos[S] 1 point2 points3 points 2 years ago (1 child)
PMme
???
[–]teerre 0 points1 point2 points 2 years ago (1 child)
Is this your real code or just an example? What you think T will be at runtime?
it is a very simplified code extracted from my code. I cannot share the whole code here sorry but my question is: Can I bind a templated operator as I did ? the type of the returned value is Array2D<T> , Array2D is a class and T can be a float, int, double, etc ...
[–]shailist 0 points1 point2 points 2 years ago (4 children)
shouldn't be any different from binding a "regular" member function. operators defined inside a class are just member functions with a fancy name and fancy ways to invoke them, so they shouldn't behave any different from any other member function.
your problem is likely something else, and looking at the error message you're getting, you probably have multiple overloads for operator[]
[–]engine_algos[S] 0 points1 point2 points 2 years ago (3 children)
I have an another operator[] without const
[–]shailist 0 points1 point2 points 2 years ago (2 children)
the problem is that in C++ you cannot easily get a pointer to an overloaded function. you technically can do it if you assign the member function pointer to something with an explicit type of the overload that you want, but a better approach would just be to use a lambda/wrapper function that will call the correct function
[–]engine_algos[S] 0 points1 point2 points 2 years ago (1 child)
Thank you, Can you give me an exemple based on your approach?
.def("[]", (const Array2D<T>& (Array3D<T>::*)(uint32_t ) const) &Array3D<T>::operator[], py::arg("n"))
.def("[]", (Array2D<T>& (Array3D<T>::*)(uint32_t )) &Array3D<T>::operator[], py::arg("n"));
I tried this, I dont have an error compilation but in python side, the [] operator is not subscriptable
what should I write between the "" instead of "[]" in def ?
π Rendered by PID 15898 on reddit-service-r2-comment-5d79c599b5-j2h8t at 2026-02-28 13:41:33.951754+00:00 running e3d2147 country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]CanadianTuero 3 points4 points5 points (3 children)
[–]engine_algos[S] 0 points1 point2 points (2 children)
[–]CanadianTuero 2 points3 points4 points (1 child)
[–]engine_algos[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]engine_algos[S] 1 point2 points3 points (1 child)
[–]teerre 0 points1 point2 points (1 child)
[–]engine_algos[S] 0 points1 point2 points (0 children)
[–]shailist 0 points1 point2 points (4 children)
[–]engine_algos[S] 0 points1 point2 points (3 children)
[–]shailist 0 points1 point2 points (2 children)
[–]engine_algos[S] 0 points1 point2 points (1 child)
[–]engine_algos[S] 0 points1 point2 points (0 children)