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_Casting_Cout - Fatal编程技术网

C++ 使用模板化声明时可能会丢失基本对象上的方法

C++ 使用模板化声明时可能会丢失基本对象上的方法,c++,templates,casting,cout,C++,Templates,Casting,Cout,这是该线程中答案的应用程序。。 其中,模板允许定义最大两个对象的类型 通过查看编译器的错误消息,我不知何故丢失了int方法 下面是一个代码: #include <iostream> template<typename A> struct givenType { typedef A type; }; template<typename A, typename B> struct largestType { typedef typename

这是该线程中答案的应用程序。。

其中,模板允许定义最大两个对象的类型

通过查看编译器的错误消息,我不知何故丢失了int方法

下面是一个代码:

#include <iostream>

template<typename A>
struct givenType {
    typedef A type;
};

template<typename A, typename B>
struct largestType {
    typedef typename givenType<std::conditional<sizeof(A) <= sizeof(B), B, A         >>::type type;
};


template<typename T1, typename T2>
class Alpha {
public:
    typedef typename largestType<T1,T2>::type bigT3;
    Alpha() {};

    bigT3 answer(void) {
        bigT3 t;
        return t;
    }

    void tryCout(void) {
        std::cout << answer() << std::endl;
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    Alpha<int,int> a;
    a.tryCout();
    return 0;
}

谢谢。

您正在尝试将整个std::conditional结构发送到std::cout Big3,现在它与std::conditional相同,类型为int

std::conditional通过其typedef类型为您提供所选类型。你应该用这个

typename std::conditional<sizeof(A) <= sizeof(B), B, A>::type
                                                         ^^^^
或者只是

template<typename T1, typename T2>
class Alpha {
public:
    typedef typename 
              std::conditional<
                sizeof(T1) <= sizeof(T2)
                , T2
                , T1
              >::type bigT3;

关于“谢谢”的错误,不能因为缺乏声誉而推翻,但效果很好。请注意,使用嵌套模板时也需要使用双typename。@user3417339是的,您需要在此处使用“typename”。然而,你似乎已经知道,因为你正在其他地方正确地使用它。这就是为什么我没有更强调它。你不能投票,但你仍然可以接受答案:
template<typename A, typename B>
struct largestType {
    typedef typename givenType<
                       typename 
                       std::conditional<
                         sizeof(A) <= sizeof(B)
                         , B
                         , A
                       >::type // You missed this type
                     >::type type;
};
template<typename A, typename B>
struct largestType {
    typedef typename 
              std::conditional<
                sizeof(A) <= sizeof(B)
                , B
                , A
              >::type type;
};
template<typename T1, typename T2>
class Alpha {
public:
    typedef typename 
              std::conditional<
                sizeof(T1) <= sizeof(T2)
                , T2
                , T1
              >::type bigT3;