我有以下结构代码:麻烦与元函数,用于识别类内模板类
template <typename T>
struct Foo
{
struct Bar
{
int data;
};
};
我想写元函数它会告诉我,如果一个类型是富或酒吧。第一个是简单的:
然而,同样的方法不适用于酒吧工作:
template <typename T>
struct is_bar : boost::mpl::false_
{};
template <typename T>
struct is_bar<typename Foo<T>::Bar> : boost::mpl::true_
{};
此代码是由编译器拒绝。 GCC说:
main.cpp:38:8: error: template parameters not used in partial specialization:
main.cpp:38:8: error: ‘T’
奇怪的是,铛将编译的代码,但它发出警告,元函数不起作用(始终为false):
main.cpp:38:8: warning: class template partial specialization contains a template parameter that can not be deduced;
this partial specialization will never be used
struct is_bar<typename Foo<T>::Bar> : boost::mpl::true_
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:37:20: note: non-deducible template parameter 'T'
template <typename T>
^
是否有针对此问题的解决方法吗?一个C++ 11特定的解决方案将会很好。
正如Clang所说,“typename Foo :: Bar'是一个不可推卸的上下文。这与要求编译器枚举所有可能的参数到'Foo'并检查是否有任何'Foo :: Bar'匹配提供的'T'是一样的。另外,'template void f(typename Foo :: bar){}'是同一种不可诱发的上下文,你必须手动指定'T'来调用这个函数。 (顺便说一句,这是如何指定类模板的部分专业化。) –
Xeo
谢谢,你的解释是有道理的。现在的问题是构建一个解决方法。 – GRedner