奇怪的模板模板参数期望错误

3
当尝试编译这段代码时:

template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
    {
        Move move;
        move.x = 1;
        move.y = 0;
        //rat.look(1, 2);
        //rat.getDna(35);
        return move;
    }
};

clang 3.2.7 报错,这是一个我不理解的奇怪错误:

main.cpp:10:28: error: template argument for template template parameter must be a class template or type alias template
        Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
                                  ^

“Dumb”是一个类模板,对吧?

正如评论中所要求的那样,这里是“rat”的样子:

template <template <class> class BRAIN, class URNG>
class Rat
{
//...
}

1
什么是 Rat 的定义? - Barry
我刚刚添加了老鼠模板的外观。 - matovitch
@πάνταῥεῖ 我想你可能是对的... :) - matovitch
1个回答

4

您遇到的问题是由注入名称引起的:

template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    // in here, "Dumb" refers to the complete type "Dumb<URNG>"
    Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
                          //  ^^^^ 
                          //  really a type, not a template

要解决这个问题,您需要引用未注入的名称,可以按照以下方式执行:
template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    Move operator()(const Rat<::Dumb, URNG>& rat, URNG&& urng)
                          //  ^^^^^^
                          //  actual Dumb<T> template, not type

太好了!谢谢!我没有考虑过在结构体内引用类型或模板的问题。 - matovitch
这是一个编译器的错误。当注入的类名被用作模板模板参数的参数时,应该将其视为模板。 - T.C.
@T.C. 啊,我没意识到这个在C++11里改了。但是对于C++03,拒绝它是正确的 - 而且不清楚OP是否正在使用C++11。 - Barry
@Barry URNG&&,暗示,暗示。(而对于C++03,您需要在::Dumb之前加一个空格 ;) ) - T.C.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接