相关疑难解决方法(0)

CRTP与模板模板参数

以下代码无法编译......

namespace {
    template<typename T, template<typename> class D>
    struct Base {
        Base(const T& _t) : t(_t) { }
        T t;
    };

    template<typename T>
    struct Derived : Base<T, Derived> {
        Derived(const T& _t) : Base<T, Derived>(_t) { }
    };
}

int main(int argc, char* argv[]) {
    Derived<int> d(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该行有一个编译错误 - Derived(const T& _t) : Base<T, Derived>(_t) { }

错误C3200''anonymous-namespace':: Derived':模板参数'D'的模板参数无效,需要一个类模板

如果我提供任何其他具有模板参数而不是Derived本身的类,则此方法有效

template<typename T>
struct Other {

};
template<typename T>
struct Derived : Base<T, Other> {
    Derived(const T& _t) …
Run Code Online (Sandbox Code Playgroud)

c++ templates crtp template-templates

18
推荐指数
2
解决办法
1376
查看次数

"Visual Studio中的模板参数无效"错误,但不是GCC

假设你有代码

template <template<class> class BaseType>
class EST16
    : public BaseType<int>
{
public:
    EST16(double d) 
    {
    }
};

template <class T>
class SCEST
{
    T y;
};
typedef EST16<SCEST> EST16_SC;
class Child
    : public EST16_SC
{
public:
    Child()
        : EST16_SC(1.0)
    {
    }

};



class NotWorkingChild
    : public EST16<SCEST>
{
public:
    NotWorkingChild()
        : EST16<SCEST>(1.0)
    {
    }

};



TEST(TemplateTest, TestInstantiate)
{
    Child child;
    NotWorkingChild notWorkingChild; 
}
Run Code Online (Sandbox Code Playgroud)

Child和NotWorkingChild仅因typedef而异.在GCC中都编译,在Visual Studio中NotWorkingChild的构造函数产生以下错误:

2>..\..\..\src\itenav\test\SCKFErrorStateTest.cpp(43) : error C3200: 'SCEST<T>' : invalid template argument for template parameter 'BaseType', expected a …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates visual-studio

8
推荐指数
1
解决办法
2610
查看次数

标签 统计

c++ ×2

templates ×2

crtp ×1

gcc ×1

template-templates ×1

visual-studio ×1