Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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_Conditional Operator - Fatal编程技术网

C++ 三元运算符

C++ 三元运算符,c++,templates,c++11,conditional-operator,C++,Templates,C++11,Conditional Operator,为什么编译器不能专门化这个函数,有没有办法强迫他这样做? 我收到的错误: 错误1错误C2893:无法专门化函数模板“未知类型”三元::检查(布尔,左,右) #include "stdafx.h" #include <iostream> #include <string> using std::cout; using std::string; template<int v> struct Int2Type { enum {value = v}; };

为什么编译器不能专门化这个函数,有没有办法强迫他这样做?
我收到的错误:
错误1错误C2893:无法专门化函数模板“未知类型”三元::检查(布尔,左,右)

#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::string;

template<int v>
struct Int2Type
{
    enum {value = v};
};

template<bool condition,class Left, class Right>
struct Result;


template<class Left, class Right>
struct Result<true,Left,Right>
{
    typedef Left value;
};

template<class Left, class Right>
struct Result<false,Left,Right>
{
    typedef Right value;
};

struct Ternary
{
    template<class Left, class Right>
    static Right check_(Int2Type<false>, Left left, Right right)
    {
        return right;
    }

    template<class Left, class Right>
    static Left check_(Int2Type<true>, Left left, Right right)
    {
        return left;
    }


__Updated__
    template<bool Condition,class Left, class Right>
static auto check(Left left, Right right)->
    typename Result<Condition,Left,Right>::value
{
    return check_(Int2Type<Condition>,left,right);
}

int _tmain(int argc, _TCHAR* argv[])
{
    int a = 5;
    string s = "Hello";
    cout << Ternary::check<false>(a,s);
    return 0;
}
#包括“stdafx.h”
#包括
#包括
使用std::cout;
使用std::string;
模板
结构Int2Type
{
枚举{value=v};
};
模板
结构结果;
模板
结构结果
{
typedef左值;
};
模板
结构结果
{
typedef右值;
};
结构三元
{
模板
静态右检查(Int2Type、左-左、右)
{
返还权;
}
模板
静态左检查(Int2Type、左、右)
{
左转;
}
__更新__
模板
静态自动检查(左、左、右)->
typename结果::值
{
返回检查(Int2Type,左,右);
}
int _tmain(int argc,_TCHAR*argv[]
{
INTA=5;
字符串s=“Hello”;

我在C++0x方面还没有足够的经验,但是从我所看到的:

    decltype(Result<(sizeof(int) == 1),Left,Right>::value)
条件
是一个变量,不能将其用作模板参数


更新:同样
Int2Type
也是一种类型。您想传递一个值:
Int2Type()

@Prasoon Saurav C++03它可以被认为是C++0x的一个“子集”,因此您的编辑不适合C++0x。是的,但也不是那么明亮。如果您搜索标记为“C++”的问题,因此不会列出此问题。g++4.5的
decltype
没有错误。它只抱怨
检查(Int2Type,左,右);
line.@KennyTM by complians您的意思是警告还是给出错误?@There:当然有错误。模板参数必须是常量表达式。@ybungalobill与sizeof运算符类似,decltype的操作数未赋值。[9]非正式地,decltype(e)返回的类型推断如下:[1]如果表达式e引用本地或命名空间范围内的变量、静态成员变量或函数参数,则结果是该变量或参数的声明类型(如果e是函数调用或重载运算符调用,则为decltype(e)表示该函数声明的返回类型,否则,如果e是左值,则decltype(e)是T&,其中T是e的类型;如果e是右值,则结果为T@There:它与求值无关。语法规定括号之间的东西必须是一个表达式,你在括号中写的是一个类型。@ybungalobill好的。我可以删除decltype-它不会compile@There:如果没有decltype无法编译,您可以尝试在结果之前添加
typename
(并删除decltype)。@ybunalobill添加typename帮助。谢谢。现在(我更新了帖子)关于检查(…)的合规性;
    return check_(Int2Type<condition>,left,right);