我从rhalbersma那里得到了这个代码,但它在VC 2010中没有编译。我不知道我做错了什么。
template<typename Derived>
struct enable_crtp
{
private:
// typedefs
typedef enable_crtp Base;
public:
// casting "down" the inheritance hierarchy
Derived const* self() const
{
return static_cast<Derived const*>(this);
}
// write the non-const version in terms of the const version
// Effective C++ 3rd ed., Item 3 (p. 24-25)
Derived* self()
{
return const_cast<Derived*>(static_cast<Base const*>(this)->self());
}
protected:
// disable deletion of Derived* through Base*
// enable deletion of Base* through Derived*
~enable_crtp()
{
// no-op
}
};
template<typename FX>
class FooInterface
:
private enable_crtp< FX >
{
public:
// interface
void foo() { self()->do_foo(); }
};
class FooImpl
:
public FooInterface< FooImpl >
{
private:
// implementation
friend class FooInterface< FooImpl > ;
void do_foo() { std::cout << "Foo\n"; }
};
class AnotherFooImpl
:
public FooInterface< AnotherFooImpl >
{
private:
// implementation
friend class FooInterface< AnotherFooImpl >;
void do_foo() { std::cout << "AnotherFoo\n"; }
};
template<template<typename> class F, int X>
class BarInterface
:
private enable_crtp< F<X> >
{
// interface
void bar() { self()->do_bar(); }
};
template< int X >
class BarImpl
:
public BarInterface< BarImpl, X >
{
private:
// implementation
friend class BarInterface< ::BarImpl, X >;
void do_bar() { std::cout << X << "\n"; }
};
int main()
{
FooImpl f1;
AnotherFooImpl f2;
BarImpl< 1 > b1;
BarImpl< 2 > b2;
f1.foo();
f2.foo();
b1.bar();
b2.bar();
return 0;
}
答案 0 :(得分:1)
template<template<typename> class F, int X>
class BarInterface
:
private enable_crtp< F<X> >
{
// interface
void bar() { self()->do_bar(); }
};
我猜
template<template<int > class F, int X>
class BarInterface
:
private enable_crtp< F<X> >
{
// interface
void bar() { self()->do_bar(); }
};
然后你需要修复关于访问私有成员函数的两个错误
template<typename Derived>
struct enable_crtp
{
private:
// typedefs
typedef enable_crtp Base;
public:
// casting "down" the inheritance hierarchy
Derived const* self() const
{
return static_cast<Derived const*>(this);
}
// write the non-const version in terms of the const version
// Effective C++ 3rd ed., Item 3 (p. 24-25)
Derived* self()
{
return const_cast<Derived*>(static_cast<Base const*>(this)->self());
}
protected:
// disable deletion of Derived* through Base*
// enable deletion of Base* through Derived*
~enable_crtp()
{
// no-op
}
};
template<typename FX>
class FooInterface
:
private enable_crtp< FX >
{
public:
// interface
void foo() { self()->do_foo(); }
};
class FooImpl
:
public FooInterface< FooImpl >
{
private:
// implementation
friend class FooInterface< FooImpl > ;
void do_foo() { std::cout << "Foo\n"; }
};
class AnotherFooImpl
:
public FooInterface< AnotherFooImpl >
{
private:
// implementation
friend class FooInterface< AnotherFooImpl >;
void do_foo() { std::cout << "AnotherFoo\n"; }
};
template<template<int > class F, int X>
class BarInterface
:
private enable_crtp< F<X> >
{
// interface
public: void bar() { self()->do_bar(); }
};
template< int X >
class BarImpl
:
public BarInterface< BarImpl, X >
{
private:
// implementation
friend class BarInterface< ::BarImpl, X >;
void do_bar() { std::cout << X << "\n"; }
};
int main()
{
FooImpl f1;
AnotherFooImpl f2;
BarImpl< 1 > b1;
BarImpl< 2 > b2;
f1.foo();
f2.foo();
b1.bar();
b2.bar();
return 0;
}
答案 1 :(得分:1)
抱歉,我为GCC-4.4重新制作了此代码,无法检查VC 2010。 错误:
模板BarInterface中的模板类声明错误 - 将typename
替换为int
:
template<template<int> class F, int X>
class BarInterface
将公开设置为foo()
和bar()
方法:
public:
void xxx() { self()->do_xxx(); }
enable_crtp<FX>
和BarInterface
中为基类FooInterface
设置公开:
public enable_crtp< FX >
self()
和foo()
方法中添加调用bar()
方法的范围说明:
void xxx() { enable_crtp<FX>::self()->do_xxx(); }
最后我得到了代码:
template<typename Derived>
struct enable_crtp
{
private:
// typedefs
typedef enable_crtp Base;
public:
// casting "down" the inheritance hierarchy
Derived const* self() const
{
return static_cast<Derived const*>(this);
}
// write the non-const version in terms of the const version
// Effective C++ 3rd ed., Item 3 (p. 24-25)
Derived* self()
{
return const_cast<Derived*>(static_cast<Base const*>(this)->self());
}
protected:
// disable deletion of Derived* through Base*
// enable deletion of Base* through Derived*
~enable_crtp()
{
// no-op
}
};
template<typename FX>
class FooInterface
:
public enable_crtp< FX >
{
public:
// interface
void foo() { enable_crtp<FX>::self()->do_foo(); }
};
class FooImpl
:
public FooInterface< FooImpl >
{
private:
// implementation
friend class FooInterface< FooImpl > ;
void do_foo() { std::cout << "Foo\n"; }
};
class AnotherFooImpl
:
public FooInterface< AnotherFooImpl >
{
private:
// implementation
friend class FooInterface< AnotherFooImpl >;
void do_foo() { std::cout << "AnotherFoo\n"; }
};
template<template<int> class F, int X>
class BarInterface
:
public enable_crtp< F<X> >
{
public:
// interface
void bar() { enable_crtp< F<X> >::self()->do_bar(); }
};
template< int X >
class BarImpl
:
public BarInterface< BarImpl, X >
{
private:
// implementation
friend class BarInterface< ::BarImpl, X >;
void do_bar() const { std::cout << X << "\n"; }
};
int main()
{
FooImpl f1;
AnotherFooImpl f2;
BarImpl< 1 > b1;
BarImpl< 2 > b2;
f1.foo();
f2.foo();
b1.bar();
b2.bar();
return 0;
}