C++ generic template method name from template parameter

x y

I am starting with variadic templates in C++11 and I wonder if its possible to create a struct / class method that gets its name from a template parameter

Something like:

MySmartTemplate<"foo", int, "bar", double> MyStruct;

// which should result in:
struct MyStruct
{
   void foo(int val){...}
   void bar(double val){...}
};

where the template parameters possibly come pairwise as <name> + <type>

Thanks

SingerOfTheFall

This is not possible with templates at all. A template in c++ is parametrized by a list of one or more template parameters. Each of them may be either:

  • a non-type template parameter;
  • a type template parameter;
  • a template template parameter.

Now, to be able to (theoretically) construct something like void foo(int val){...}, you need to pass the name of "foo" and it's parameter type into our imaginary template. While passing the type of val is not a problem, passing "foo"s name is impossible. The only non-type template parameters that you can use are:

  • std::nullptr_t;
  • integral type;
  • lvalue reference type (to object or to function);
  • pointer type (to object or to function);
  • pointer to member type (to member object or to member function);
  • enumeration type.

You should also note that the non-type template parameters of reference and pointer types have a few exceptions, namely they can not refer to or be an address of a string literal (related: this question). Considering the above, there is no way to pass a string literal to a template, and thus it is impossible to achieve what you want with templates.

On a side note, while variadic templates are a nice addition to the language, they have a few limitations, namely you can only perform the expansion of the parameter pack, but you can't address individual parameters in the pack.

So, to conclude, no, what you want is not possible with templates.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ template name used without template parameter

From Dev

C++ template self name passing as a template template parameter inside of same template structure function

From Dev

C++ Template with class method as parameter

From Dev

Template parameter name hiding

From Dev

moq a method with a template parameter

From Dev

Calling a template method from another template method in C++?

From Dev

CLI/C++ Interface class, Generic method calling template method

From Dev

CLI/C++ Interface class, Generic method calling template method

From Dev

C++ Template Parameter that evaluates a Template (template template parameter)

From Dev

C# generic compare method [analogue of C++ template]

From Dev

C++: Inherit class from template parameter

From Dev

C++: Inheritance from template parameter

From Dev

Template class with method depending on template parameter

From Dev

typedef and template parameter with same name

From Dev

Add method to class by template parameter

From Dev

Add method to class by template parameter

From Dev

Select a method as (default) template parameter

From Dev

C++ template function for method with a parameter of a const type or simply of type

From Dev

generic lambda auto in parameter type template type

From Dev

C++ function template overload on template parameter

From Dev

C++ name lookup affected by declaration of a template method

From Dev

Getting type from template template method parameters

From Dev

Calling a static template method from a template function

From Dev

c++ template: boost::mpl::transform with template template parameter

From Dev

Using function template as template template parameter in C++

From Dev

Compiler confuses name of an (unrelated) template with method name

From Dev

Compiler confuses name of an (unrelated) template with method name

From Dev

Compile time array from C++ template parameter pack

From Dev

c++ Unpacking parameter pack from template arguments

Related Related

  1. 1

    C++ template name used without template parameter

  2. 2

    C++ template self name passing as a template template parameter inside of same template structure function

  3. 3

    C++ Template with class method as parameter

  4. 4

    Template parameter name hiding

  5. 5

    moq a method with a template parameter

  6. 6

    Calling a template method from another template method in C++?

  7. 7

    CLI/C++ Interface class, Generic method calling template method

  8. 8

    CLI/C++ Interface class, Generic method calling template method

  9. 9

    C++ Template Parameter that evaluates a Template (template template parameter)

  10. 10

    C# generic compare method [analogue of C++ template]

  11. 11

    C++: Inherit class from template parameter

  12. 12

    C++: Inheritance from template parameter

  13. 13

    Template class with method depending on template parameter

  14. 14

    typedef and template parameter with same name

  15. 15

    Add method to class by template parameter

  16. 16

    Add method to class by template parameter

  17. 17

    Select a method as (default) template parameter

  18. 18

    C++ template function for method with a parameter of a const type or simply of type

  19. 19

    generic lambda auto in parameter type template type

  20. 20

    C++ function template overload on template parameter

  21. 21

    C++ name lookup affected by declaration of a template method

  22. 22

    Getting type from template template method parameters

  23. 23

    Calling a static template method from a template function

  24. 24

    c++ template: boost::mpl::transform with template template parameter

  25. 25

    Using function template as template template parameter in C++

  26. 26

    Compiler confuses name of an (unrelated) template with method name

  27. 27

    Compiler confuses name of an (unrelated) template with method name

  28. 28

    Compile time array from C++ template parameter pack

  29. 29

    c++ Unpacking parameter pack from template arguments

HotTag

Archive