How do I "enable" a function based on a template parameter?

Gasim

I have a a template function:

template<class Route, bool enabled=true>
void addRoute() {
    std::cout << "Route added";
}

I want to disable the function if enabled=false. I know there is no way to disable calling of a function from parameters unless using macros. However, since this function will be called once through the lifetime of the program, I don't mind calling an empty function if enabled is set to false. So, I tried the first thing that came to mind:

template<class Route>
void addRoute<false>() { }

This failed, so I did some digging and found out that partial template specialization is not possible; I can create a partially specialized class with arguments and do the same thing.

Is there another way to do this problem or should I stick with creating a class with static function?

Daniel Frey

How do you want to "disable" the function if you explicitly specify the second parameter? It's true by default and it's not deduced, so the only way to call this function is via

addRoute<RouteType, false>();

In that context, "disabled" could only mean it generates an error, right? If you want that, just add

static_assert( enabled, "Not enabled!" );

to the function's body as the first line. Or if you want to just "skip" the code inside, you can simply add

if( !enabled ) return;

as the first line. The optimizer will make sure that no superfluous code will be generated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I disambiguate this call to a generic function with a function parameter in Swift?

From Dev

How do I use std::enable_if to enable or disable constructors depending on template types?

From Dev

How do I pass an event as a function parameter?

From Dev

How do I "enable" a function based on a template parameter?

From Dev

How do I stay DRY with Meteor template helpers function composition?

From Dev

How do I terminate variadic template recursion based on the number of elements?

From Dev

How can I call a function in a template based on a component?

From Dev

In C++ 11, how do I specialize a function template that takes a function object based on return type?

From Dev

How template function chooses parameter?

From Dev

How do I enable_if a class with variadic template arguments?

From Dev

Wordpress - How can I enable pagination on template?

From Dev

How do I pass a template that's inside a template to another template as a template template parameter?

From Dev

Enable Template Ctor based on template parameter

From Dev

Enable method based on boolean template parameter

From Dev

How do I call a controller function from a template in Ember?

From Dev

How do I pass in a table parameter to this function?

From Dev

How do i pass a dictionary as a function parameter?

From Dev

How do I create a template function for controls of a form?

From Dev

How to use enable_if as template parameter with specialization

From Dev

How do I declare, send classes with unknown template argument into function?

From Dev

how do I specialize a bound template friend function to a template class?

From Dev

how do I access a named parameter in a function?

From Dev

How do I stay DRY with Meteor template helpers function composition?

From Dev

How do I terminate variadic template recursion based on the number of elements?

From Dev

How Do I Make a STL Stack Use a Template Parameter?

From Dev

WPF How do I add items to a window that was based on a windows template?

From Dev

How do I pass in a parameter to a function in an array?

From Dev

How do I correctly use enable_if in this specific function declaration?

From Dev

How do I turn the fucntion sumVec into a template function

Related Related

  1. 1

    How do I disambiguate this call to a generic function with a function parameter in Swift?

  2. 2

    How do I use std::enable_if to enable or disable constructors depending on template types?

  3. 3

    How do I pass an event as a function parameter?

  4. 4

    How do I "enable" a function based on a template parameter?

  5. 5

    How do I stay DRY with Meteor template helpers function composition?

  6. 6

    How do I terminate variadic template recursion based on the number of elements?

  7. 7

    How can I call a function in a template based on a component?

  8. 8

    In C++ 11, how do I specialize a function template that takes a function object based on return type?

  9. 9

    How template function chooses parameter?

  10. 10

    How do I enable_if a class with variadic template arguments?

  11. 11

    Wordpress - How can I enable pagination on template?

  12. 12

    How do I pass a template that's inside a template to another template as a template template parameter?

  13. 13

    Enable Template Ctor based on template parameter

  14. 14

    Enable method based on boolean template parameter

  15. 15

    How do I call a controller function from a template in Ember?

  16. 16

    How do I pass in a table parameter to this function?

  17. 17

    How do i pass a dictionary as a function parameter?

  18. 18

    How do I create a template function for controls of a form?

  19. 19

    How to use enable_if as template parameter with specialization

  20. 20

    How do I declare, send classes with unknown template argument into function?

  21. 21

    how do I specialize a bound template friend function to a template class?

  22. 22

    how do I access a named parameter in a function?

  23. 23

    How do I stay DRY with Meteor template helpers function composition?

  24. 24

    How do I terminate variadic template recursion based on the number of elements?

  25. 25

    How Do I Make a STL Stack Use a Template Parameter?

  26. 26

    WPF How do I add items to a window that was based on a windows template?

  27. 27

    How do I pass in a parameter to a function in an array?

  28. 28

    How do I correctly use enable_if in this specific function declaration?

  29. 29

    How do I turn the fucntion sumVec into a template function

HotTag

Archive