c – enable_if添加具有默认参数的函数参数?

我无法理解 here提出的第二个场景.它说:

•Scenario 2: Adding a function parameter that has a default argument:

template <your_stuff> your_return_type_if_present
yourfunction(args,enable_if_t<your condition,FOO> = BAR) {
    // ...
}

Scenario 2 leaves the parameter unnamed. You Could say ::type Dummy = BAR,but the name Dummy is irrelevant,and giving it a name is likely to trigger an unreferenced parameter warning. You have to choose a FOO function parameter type and BAR default argument. You Could say int and 0,but then users of your code Could accidentally pass to the function an extra integer that would be ignored. Instead,we recommend that you use void ** and either 0 or nullptr because almost nothing is convertible to void **:

template <your_stuff> your_return_type_if_present 
yourfunction(args,typename enable_if<your_condition,void **>::type=nullptr) {
 // ...
}

如果方案2使参数未命名,那么它可以使用什么?
有没有办法让这样的代码与enable_if一起工作?

enum otype {oadd,omull};
template<otype o>
int add(int num1,std::enable_if<o == oadd,int>::type int= num2)
{
    if (o == omull) return num1 * num1;
    if (o == oadd ) return num1 + num2;
 }

解决方法

enable_if示例(如果有帮助):

对于具有非void返回类型的函数

对于单一条件:

template <template T,typename std::enable_if<!std::is_same<T,std::string>::value>::type* = nullptr >
T func(T x){}

对于多种情况:

template <template T,std::string>::value &&!std::is_same<T,int>::value>::type* = nullptr >
T func(T x){}

对于具有void返回类型的函数

对于单一条件:

template <template T>
typename std::enable_if<!std::is_same<T,std::string>::value>::type
func(T x){}

对于多种情况:

template <template T>
typename std::enable_if<!std::is_same<T,int>::value>::type
func(T x){}

不要忘记包含#include< type_traits>

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...