Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++;具有模板化类型struct woes的函数模板参数_C++_Function_Templates_C++11_Arguments - Fatal编程技术网

C++ C++;具有模板化类型struct woes的函数模板参数

C++ C++;具有模板化类型struct woes的函数模板参数,c++,function,templates,c++11,arguments,C++,Function,Templates,C++11,Arguments,此代码无法编译: struct sometype { template <typename T> T* get() { return nullptr; } }; template <typename T> struct anothertype { #if 1 template <typename T2> struct some_wrapper { typedef T2 type; }; typedef typename som

此代码无法编译:

struct sometype
{
    template <typename T>
    T* get() { return nullptr; }
};

template <typename T>
struct anothertype
{
#if 1
    template <typename T2> struct some_wrapper { typedef T2 type; };
    typedef typename some_wrapper<sometype>::type thetype;
#else
    typedef sometype thetype;
#endif
    typedef thetype* Ptr;

    Ptr m_ptr;
    T* get() { return m_ptr->get<T>(); }
};

你的TypeDef很难说,但我敢打赌你需要:

m_ptr->template get<T>();
m_ptr->template get();

您应该按如下方式修复函数调用,添加
模板
关键字:

T* get() { return m_ptr->template get<T>(); }
T*get(){return m_ptr->template get();}

您可以查看语法解释。

如果您运行的代码与通过编译器发布的代码完全相同,是否会出现错误?@DavidRodríguez dribeas(与问题中的代码完全相同),但是。。。为什么?
m_ptr
总是
sometype*
@ipc他有
\if 1
这将是真的,所以
m_ptr
typename someu包装器::type*
,一个依赖于什么的类型?“表达式可能依赖于类型(取决于模板参数的类型)”。
some_wrapper::type*
@ipc中没有涉及模板参数:不明显的是,为什么会有不同。。。之所以重要,是因为
some\u wrapper
实际上是
另一种类型::some\u wrapper
,因此依赖于
some\u wrapper
的任何名称都会成为依赖名称,要解析依赖名称,您需要额外的
typename
template
关键字。[与
typedef typename some_wrapper::type thetype;
]@ipc中需要typename的方式完全相同:@ipc:我上一条评论的推论,这一点可能也不明显,即您可以在定义之后更改嵌套类型/模板的内容:
template struct A{struct B{int x;};static int size(){return sizeof(B);};模板结构A::B{int y[2];};int main(){std::cout
T* get() { return m_ptr->template get<T>(); }