问题描述
(稍微)过时的探讨了使用 decltype
以及SFINAE来检测类型是否支持某些运算符,例如 ==
或<
。
A (somewhat) outdated article explores ways to use decltype
along with SFINAE to detect if a type supports certain operators, such as ==
or <
.
以下是检测类是否支持<
运算符的示例代码:
Here's example code to detect if a class supports the <
operator:
template <class T>
struct supports_less_than
{
static auto less_than_test(const T* t) -> decltype(*t < *t, char(0))
{ }
static std::array<char, 2> less_than_test(...) { }
static const bool value = (sizeof(less_than_test((T*)0)) == 1);
};
int main()
{
std::cout << std::boolalpha << supports_less_than<std::string>::value << endl;
}
这会输出 true
,因为当然 std :: string
支持<
运算符。但是,如果我尝试使用不支持<
运算符的类,则会出现编译错误:
This outputs true
, since of course std::string
supports the <
operator. However, if I try to use it with a class that doesn't support the <
operator, I get a compiler error:
error: no match for ‘operator<’ in ‘* t < * t’
所以SFINAE不工作。我试过这个在GCC 4.4和GCC 4.6,和两个展示相同的行为。那么,是否可以使用SFINAE以这种方式检测类型是否支持某些表达式?
So SFINAE is not working here. I tried this on GCC 4.4 and GCC 4.6, and both exhibited the same behavior. So, is it possible to use SFINAE in this manner to detect whether a type supports certain expressions?
推荐答案
您需要将less_than_test函数作为模板,因为SFINAE表示替换失败不是错误,可能会在您的代码中失败选择。
You need to make your less_than_test function a template, since SFINAE stands for Substitution Failure Is Not An Error and there's no template function that can fail selection in your code.
template <class T>
struct supports_less_than
{
template <class U>
static auto less_than_test(const U* u) -> decltype(*u < *u, char(0))
{ }
static std::array<char, 2> less_than_test(...) { }
static const bool value = (sizeof(less_than_test((T*)0)) == 1);
};
int main()
{
std::cout << std::boolalpha << supports_less_than<std::string>::value << endl;
}
这篇关于使用decltype / SFINAE检测操作员支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!