相关疑难解决方法(0)

boost :: enable_if不在函数签名中

这只是一个关于样式的问题:我不喜欢C++模板元编程的方式,它要求你使用返回类型或为SFINAE的技巧添加额外的伪参数.所以,我提出的想法是将SFINAE事物放在模板参数定义本身中,如下所示:

#include <iostream>
#include <boost/type_traits/is_array.hpp>
#include <boost/utility/enable_if.hpp>
using namespace std;

template <typename T, typename B=typename boost::enable_if< boost::is_array<T> >::type > void asd(){
    cout<<"This is for arrays"<<endl;
}

template <typename T, typename B=typename boost::disable_if< boost::is_array<T> >::type > void asd(){
    cout<<"This is for NON arrays"<<endl;
}

int main() {
    asd<int>();
    asd<int[]>();
}
Run Code Online (Sandbox Code Playgroud)

这个例子让g ++抱怨:

../src/afg.cpp:10:97:错误:重新定义'template void asd()'

SFINAE就在那里工作,因为如果我删除例如一个disable_if,编译错误是:

../src/afg.cpp:15:12:错误:没有匹配函数来调用'asd()'

这就是我想要的.

那么,有没有办法在一个函数的"正常"签名中完成SFINAE,即返回类型+参数列表?

编辑:这最终我将在真正的代码中尝试:

#include <iostream>
#include <type_traits>
using namespace std;

template <typename T, typename enable_if< is_array<T>::value, int >::type =0 > void asd(){ …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11

7
推荐指数
3
解决办法
3013
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1