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

时间:2015-01-24 14:38:38

标签: c++ templates c++11 template-templates

尝试编译这段代码时:

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;
    }
};
叮声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是一个类模板不是吗?

正如评论中所说,这就是老鼠的样子:

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

1 个答案:

答案 0 :(得分: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