这个最小的可编译示例似乎是SFINAE的一个非常标准的设置
#include <type_traits>
struct AType{};
// Helper type-trait templates for AType
template<typename T> struct isAType{ static const bool value = false; };
template<> struct isAType<AType>{ static const bool value = true; };
template<typename T>
void function( typename std::enable_if<isAType<T>::value, T>::type& t
) {}
int main()
{
AType a1;
// This line, using automatic type deduction, fails to compile:
// function( a1 );
// If you manually specify template parameter, it compiles:
function<AType>( a1 );
}当function( a1 );未被注释时,我得到的错误消息如下:
main.cpp: In function ‘int main()’:
main.cpp:17:16: error: no matching function for call to ‘function(AType&)’
function( a1 );
^
main.cpp:10:6: note: candidate: template<class T> void function(typename
std::enable_if<isAType<T>::value, T>::type&)
void function( typename std::enable_if<isAType<T>::value, T>::type& t )
{}
^
main.cpp:10:6: note: template argument deduction/substitution failed:
main.cpp:17:16: note: couldn't deduce template parameter ‘T’
function( a1 );我见过some posts表示"T“处于非演绎的上下文中。“非演绎上下文”对我来说是一个新概念,但在其他地方已经溢出了足够多的墨水,我可以弄清楚。我想,我这里的问题是,我的function声明是否可以调整到这样一种方式,即自动类型推导将成功。有没有一种规范的方法来实现带有类型特征的SFINAE,以便自动类型推断成功?
发布于 2017-11-24 03:02:48
并不是所有的C++编译器都支持它,但是如果你的编译器支持它,这是最干净的方式:
template<bool b>
using sfinae = typename std::enable_if< b, bool >::type;
template<class T,
sfinae<isAType<T>::value> =true
>
void function( T& t )
{
}在c++14中,我不会为sfinae别名而烦恼,但在c++11中去掉typename是值得的。
注意,=true部分是必需的,但是如果它是=false,那么它的含义也是一样的。
这里发生的事情是,我们定义了一个非类型模板参数,它的类型只有在测试通过时才存在。然后我们给它一个默认值。
我发现这种技术读起来最像传入的c++17,我指的是c++20,我指的是C++的c++23概念特性。
https://stackoverflow.com/questions/47461558
复制相似问题