具有模板参数和crtp的类,仅按类名称进行构造函数调用

时间:2019-05-19 19:08:23

标签: c++ templates crtp

我不确定应该搜索什么,因此,如果已经提出此问题,我感到抱歉。

我有以下代码

namespace ns {
    template<typename crtp> struct  base_type {
        template<typename ...Args> base_type(Args &&...args);
    };
}

struct derived_type: ns::base_type<derived_type> {
    derived_type(int a, float b): base_type{a, b} {}
};

template<typename template_arg>
struct derived_template_type: ns::base_type<derived_template_type<template_arg>> {
    derived_template_type(int a, float b): base_type{a, b} {}
};

derived_type编译正常。 derived_template_type无法编译并出现以下错误(clang,c ++ 17):

error: member initializer 'base_type' does not name a non-static data member or base class
    derived_template_type(int a, float b): base_type{a, b} {}
                                           ^~~~~~~~~~~~~~~

要使其工作,我必须用完全限定的名称base_type来代替ns::base_type<derived_template_type<template_arg>>的用法-因此基本上重复基类列表中已经存在的内容。

第一个问题是:为什么会这样?编译器应该知道这是基类,知道它的位置以及模板参数是什么(例如在derived_type的情况下),但是显然不是。哪种语言规则会导致这种行为?

第二个问题(显然)是:我可以做些事情来避免这种重复吗?

0 个答案:

没有答案