Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在编译时将隐式转换为整型(最好是布尔型)_C++_Templates_C++11_Template Meta Programming - Fatal编程技术网

C++ 在编译时将隐式转换为整型(最好是布尔型)

C++ 在编译时将隐式转换为整型(最好是布尔型),c++,templates,c++11,template-meta-programming,C++,Templates,C++11,Template Meta Programming,我们有一个完整的值包装器。例如,像std::true\u type和std::false\u type这样的布尔包装器: template<typename T , T VALUE> struct integral_value_wrapper { static const T value = VALUE; }; template<bool VALUE> using boolean_wrapper = integral_value_wrapper<bool,

我们有一个完整的值包装器。例如,像
std::true\u type
std::false\u type
这样的布尔包装器:

template<typename T , T VALUE>
struct integral_value_wrapper
{
    static const T value = VALUE;
};

template<bool VALUE>
using boolean_wrapper = integral_value_wrapper<bool,VALUE>;

using true_wrapper  = boolean_wrapper<true>;
using false_wrapper = boolean_wrapper<false>;

不能提供需要表达式的类型。但如果向包装器添加转换运算符,如下所示:

template<typename T , T VALUE>
struct integral_value_wrapper
{
    static constexpr T value = VALUE;
    constexpr operator T () const { return value; }
};
模板
结构整型值包装器
{
静态constexpr T值=值;
constexpr运算符T()const{return value;}
};
然后你可以写:

if ( is_int<type>() )
//               ^^
if(is_int())
//               ^^

这就是标准类型traits所做的。

我知道转换运算符,可以像函子一样使用整型包装器。但我想要一个隐式转换,像整数值一样使用包装器。你觉得这个建议怎么样?:能帮我解决这个问题吗?@Manu343726:
我知道转换运算符,可以像函子一样使用整型包装器。不,你很困惑。首先,重点是这些包装器是类型。不能使用像值这样的类型。这是毫无意义的(如果这听起来很刺耳,我很抱歉)。该提案是关于引入一个新的调用操作符,这将允许将其用作函子。但这仍然意味着您必须首先创建该类型的实例,因此:
是_int()()
。请使用
::value
或按回答中所示实例化包装器OK,谢谢。真正地使用一个值的包装器来使用它,就像一个值听起来有点。。。循环的我在寻找更清晰的语法。非常感谢。@Manu343726:没问题,很高兴我能帮上忙——至少我又读了一遍。希望现在我们有了C++14模板别名!
template<typename T , T VALUE>
struct integral_value_wrapper
{
    static constexpr T value = VALUE;
    constexpr operator T () const { return value; }
};
if ( is_int<type>() )
//               ^^