Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++_C++11_Stl_Compile Time - Fatal编程技术网

C++ 使用分支逻辑进行编译时类型检查

C++ 使用分支逻辑进行编译时类型检查,c++,c++11,stl,compile-time,C++,C++11,Stl,Compile Time,我试图确定是否有一种方法,在编译时,同时对两种不同的类型进行条件检查 例如: template <typename T> class MyClass { returnVal myFunction() const { // Is there a compile time way of doing this? std::conditional? return myConditionFunction(std::is_same<char

我试图确定是否有一种方法,在编译时,同时对两种不同的类型进行条件检查

例如:

template <typename T>
class MyClass
{
    returnVal myFunction() const
    {
        // Is there a compile time way of doing this?  std::conditional?
        return myConditionFunction(std::is_same<char, T> || std::is_same<unsigned char, T>);
    }

    returnVal myConditionFunction(std::true_type& const) const
    {
        // perform calculations on char or unsigned char
    }

    returnVal myConditionFunction(std::false_type& const) const
    {
        // perform calculations on non-char/unsigned char types
    }
};
模板
类MyClass
{
returnVal myFunction()常量
{
//有没有编译时的方法来实现这一点?std::conditional?
返回myConditionFunction(std::is|u same | std::is_same);
}
returnVal myConditionFunction(std::true_type&const)const
{
//对字符或无符号字符执行计算
}
returnVal myConditionFunction(std::false_type&const)const
{
//对非字符/无符号字符类型执行计算
}
};
如果类型是char或unsigned char,我想调用一个函数


编辑:更新代码以显示我正在使用模板化类。

有很多方法可以实现您想要的功能。如果您不想自己编写任何新模板,可以这样做(伪代码,因为没有指定
returnVal
,也没有从任何地方指定或推导出
t
):

然后像这样使用它:

returnVal myFunction() const
{
    return myConditionFunction(typename is_char_or_uchar<T>::type());
}

returnVal myConditionFunction(std::true_type& const) const
{
}

returnVal myConditionFunction(std::false_type& const) const
{
}
returnVal myFunction()常量
{
返回myConditionFunction(typename为_char_或_uchar::type());
}
returnVal myConditionFunction(std::true_type&const)const
{
}
returnVal myConditionFunction(std::false_type&const)const
{
}

或者,如果您有Boost,您可以使用和两个调用
std::is_same

您可以使用条件生成编译时常量,并创建与
true_type
false_type
匹配的类型:

returnVal myFunction() const
{
   typedef std::integral_constant<bool, 
                                  std::is_same<unsigned char, T>::value
                               || std::is_same<         char, T>::value> selector;

   return myConditionFunction(selector());
}
returnVal myFunction()常量
{
typedef std::integral_constant::value>选择器;
返回myConditionFunction(选择器());
}

请注意,
std::true_type
只是
std::integral_常量
,对于
std::false_type
,您当前正在做的是编译时的事情。但是(std::is_same | std::is_same)不起作用。我可以做一个或另一个,但不能同时做两个。您专门化的char和unsigned char的模板如何?第一个选项快速尝试,我在使用模板时会出现编译错误,在使用std::make_unsigned()之前,我需要保证类型T不是布尔型。
returnVal myFunction() const
{
    return myConditionFunction(typename is_char_or_uchar<T>::type());
}

returnVal myConditionFunction(std::true_type& const) const
{
}

returnVal myConditionFunction(std::false_type& const) const
{
}
returnVal myFunction() const
{
   typedef std::integral_constant<bool, 
                                  std::is_same<unsigned char, T>::value
                               || std::is_same<         char, T>::value> selector;

   return myConditionFunction(selector());
}