Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?

后端 未结 2 1323
耶瑟儿~
耶瑟儿~ 2020-11-30 11:04

In the language reference of std::enable_if at cppreference the following note is included

Notes

A common mistake is to d

相关标签:
2条回答
  • 2020-11-30 11:45

    The common mistake is:

    template <typename T, typename = std::enable_if_t<cond>>
    void foo()
    
    template <typename T, typename = std::enable_if_t<!cond>>
    void foo()
    

    which both declare

    template <typename, typename>
    void foo();
    
    0 讨论(0)
  • 2020-11-30 11:48

    Notes

    A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.

    Your functions don't differ only in their default template arguments, they differ in their template parameters, so have different signatures.

    In both cases the default template argument is nullptr, but the second template parameter is different in each case.

    0 讨论(0)
提交回复
热议问题