Search code examples
c++templatesclass-template

Can C++ class templates take method names as template parameters?


Just like the title asks, can C++ class templates take method names as template parameters?

For instance,

template <T>
class Foo
{
public:
    void T(int bar);
};

Solution

  • Unfortunately, the C++ core language does not have any means of handling names.

    Some possibilities:

    • Handle names via the preprocessor, ie., ugly macros.
      Note: the Boost parameters library uses some undocumented Boost macro trickery that is very relevant here. I used that for a general options class thing once. Sorry I can't remember much about it, but essentially it supports a kind of variadic macros for C++03.
    • Do your own custom preprocessing, i.e. script based.
    • Put the burden on the client code programmer, somehow.

    For those who wonder, what does one need this for?, a case in point is how to something like Python's library "named tuple" class, in C++, where the programmer provides the names of the tuple members.