c – 无法使用auto to parametrize true_type检测T :: value()

c – 无法使用auto to parametrize true_type检测T :: value(),第1张

概述使用SFINAE,has_value_int< T>和has_value_auto< T>两者都试图检测类T是否具有名为value的静态constexpr函数. >使用int来参数化true_type,has_value_int< T>效劳于 演示类传递和失败. >使用auto来参数化true_type,has_value_auto< T>总是返回false. 使用int和使用auto有什么区别, 使用SFINAE,has_value_int< T>和has_value_auto< T>两者都试图检测类T是否具有名为value的静态constexpr函数.

>使用int来参数化true_type,has_value_int< T>效劳于
演示类传递和失败.
>使用auto来参数化true_type,has_value_auto< T>总是返回false.

使用int和使用auto有什么区别,为什么auto不能工作?

具体来说,为什么重载解析更喜欢match_auto(…)到match_auto(int)?

#include <iostream>using namespace std;// parametrize true_type template <int> using true_int = true_type; // workstemplate <auto> using true_auto = true_type; // does not work// detect if T::value() is a valID compile-time Expressiontemplate <class T> true_int<(T::value(),voID(),0)> match_int(int);template <class T> true_auto<(T::value(),0)> match_auto(int);template <class> false_type match_int(...); // sometimes calledtemplate <class> false_type match_auto(...); // always calledtemplate <class T>static constexpr bool has_value_int = decltype(match_int<T>(0))::value;template <class T>static constexpr bool has_value_auto = decltype(match_auto<T>(0))::value;template <class T>voID demo() {    cout << has_value_int<T> << "(int)," // sometimes false            << has_value_auto<T> << "(auto)" << endl; // always false}int main() {    struct pass { static constexpr int value() { return 1; } };    using fail = float;    cout << "has_value<pass> = "; demo<pass>(); // 1(int),0(auto)    cout << "has_value<fail> = "; demo<fail>(); // 0(int),0(auto)    return 0;}

编辑:Compiled using gcc 7.3.0.
与clang works相同.

解决方法 没有区别;这是一个gcc BUG,由barry报道为 #86703.

作为解决方法,请勿使用以下功能:

// detect if T::value() is a valID compile-time Expressiontemplate <typename T,typename = voID> struct match_auto : std::false_type {};template <typename T>struct match_auto<T,std::voID_t<true_auto<(T::value(),0)>>>    : std::true_type {};template <class T> static constexpr bool has_value_auto = match_auto<T>::value;
总结

以上是内存溢出为你收集整理的c – 无法使用auto to parametrize true_type检测T :: value()全部内容,希望文章能够帮你解决c – 无法使用auto to parametrize true_type检测T :: value()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1238229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存