C ++ MSVC / GCC编译问题与派生类的成员函数特化

时间:2014-07-28 14:44:33

标签: c++ templates derived-class specialization member-functions

为什么以下代码使用gcc(4.6.3)进行编译,但在使用Microsoft Visual Studio 2013时,会导致第二个成员函数SomeFunction已定义或声明的错误?我需要更改什么,以便此示例适用于gcc和Microsoft编译器?

非常感谢你的帮助。

template <bool B = false>
class Base {
public:
    Base() {};
    ~Base() {};
    template <bool U> struct i2t{};
    typedef i2t<B> SomeType;
    void Test1(i2t<true>) {
        std::cout << "true" << std::endl;
    }
    void Test1(i2t<false>) {
        std::cout << "false" << std::endl;
    }
    void Test() {
        Test1(SomeType());
    }
};

template <class N=Base<false> >
class Derived : public N {
public:
    Derived() {};
    ~Derived() {};
    void SomeFunction(typename N::template i2t<true>) {
        std::cout << "true" << std::endl;
    }
    void SomeFunction(typename N::template i2t<false>) {
        std::cout << "false" << std::endl;
    }
    void Caller() {
        SomeFunction(typename N::SomeType());
    }
};

int main(int argc, char** argv) {
    class Base<false> test;
    test.Test();
    class Base<true> test1;
    test1.Test();
    class Derived<Base<false> > test2;
    test2.Caller();
    class Derived<Base<true> > test3;
    test3.Caller();

    return 0;
}

0 个答案:

没有答案