以下代码片段演示了我想要实现的内容,即创建两个模板特化(好吧,这里是主模板和专业化),将使用一个用于非const成员函数,一个用于const成员函数:如何重写它使其符合C++标准
// instantiate for non-const member functions
template <typename C, void(C::*F)()>
struct A {};
// instantiate for const member functions
template <typename C, void(C::*F)() const>
struct A<C const, F> {};
struct foo
{
void bar() const {}
typedef A<foo const, &foo::bar> bar_type;
void baz() {}
typedef A<foo, &foo::baz> baz_type;
};
尽管此代码编译好使用gcc 4.7,英特尔13.0和MSVC 2012,它失败使用锵3.3或科莫4.3.10.1进行编译。我相信克朗其实是对的。
如何重写此代码以使其符合标准(即与Clang编译)?
以下是编译错误:
test_6.cpp:22:26: error: non-type template argument of type 'void (foo::*)() const' cannot be converted to a value of type 'void (const foo::*)()'
typedef A<foo const, &foo::bar> bar_type;
^~~~~~~~~
test_6.cpp:7:33: note: template parameter is declared here
template <typename C, void (C::*F)()>
^
为什么不向我们展示编译错误?为什么不提供版本信息? – 2013-03-04 00:31:43
*“无法编译”* ...带有错误信息? – 2013-03-04 00:31:54
我添加了编译器版本和Clang错误消息。 – hkaiser 2013-03-04 00:35:18