模板:模板功能不能很好地与类的模板成员函数 [英] Templates: template function not playing well with class's template member function

查看:83
本文介绍了模板:模板功能不能很好地与类的模板成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我实际拥有的一些代码的最小测试用例。当它尝试评估 a.getResult< B>()时会失败:

  test.cpp:在函数'void printStuff(const A&)':
test.cpp:6:错误:预期的主表达式'>'token
test.cpp:6 :error:expected-expression before')'token

代码是:

  #include< iostream> 

template< A类,B类>
void printStuff(const A& a)
{
size_t value = a.getResult< B>();
std :: cout<<值<< std :: endl;
}

struct Firstclass {
template<类X>
size_t getResult()const {
X someInstance;
return sizeof(someInstance);
}
};

int main(int,char **){
Firstclass foo;

printStuff< Firstclass,short int>(foo);
printStuff< Firstclass,double>(foo);

std :: cout<< foo.getResult< double>()<< std :: endl;

return 0;
}



如果我注释掉了 printStuff 函数,并且它被调用, foo.getResult< double>()调用编译正确,并且执行预期。



任何想法发生了什么事?

解决方案

当你引用一个模板,它是依赖类型的成员,你必须用一个关键字模板前面。这是如何调用 getResult 里面 printStuff 应该看起来

  size_t value = a.template getResult< B>(); 

这类似于使用关键字 typename 当引用依赖类型中的嵌套类型名时。由于某种原因, typename 与嵌套类型的位是相当知名的,但模板与类似的要求嵌套模板是相对未知的。



请注意,一般的语法结构有点不同。 typename 始终放在类型的全名前面,而 template 插入到中间。 / p>

同样,这只有在您访问依赖类型的模板成员时才需要,在上面的示例中,它是 A 在 printStuff 中。当您在中调用 foo 的类型 foo.getResult< c $ c>不依赖,因此不需要包含模板关键字。


This is a minimal test case of some code that I actually have. It fails when it tries to evaluate a.getResult<B>():

test.cpp: In function 'void printStuff(const A&)':
test.cpp:6: error: expected primary-expression before '>' token
test.cpp:6: error: expected primary-expression before ')' token

The code is:

#include <iostream>

template< class A, class B>
void printStuff( const A& a)
{
    size_t value = a.getResult<B>();
    std::cout << value << std::endl;
}

struct Firstclass {
    template< class X >
    size_t getResult() const {
        X someInstance;
        return sizeof(someInstance);
    }
};

int main(int, char**) {
    Firstclass foo;

    printStuff<Firstclass, short int>(foo);
    printStuff<Firstclass, double>(foo);

    std::cout << foo.getResult< double >() << std::endl;

    return 0;
}

If I comment out the printStuff function and where it's called, the foo.getResult< double >() call compiles fine and does what is expected.

Any idea what's going on? I've been working with extensively templated code for a while and have never encountered anything like this.

解决方案

When you refer to a template that is a member of dependent type, you have to prepend it with a keyword template. This is how the call to getResult inside printStuff should look

size_t value = a.template getResult<B>();

This is similar to using the keyword typename when referring to nested typenames in a dependent type. For some reason, the bit about typename with nested types is rather well-known, but the similar requirement for template with nested templates is relatively unknown.

Note that the general syntax structure is a bit different though. The typename is always put in front of the full name of the type, while template is inserted in the middle.

Again, this is only necessary when you are accessing a template member of a dependent type, which in the above example would be A in printStuff. When you call foo.getResult<> in main the type of foo is not dependent, so there's no need to include the template keyword.

这篇关于模板:模板功能不能很好地与类的模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆