简体   繁体   中英

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);
};

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM