我在分离内部类的实现同时具有部分特化时遇到问题。这是说明我的问题的示例代码:
#include <type_traits>
template <typename T>
using enable_if_copyable = std::enable_if_t<std::is_copy_constructible<T>::value>;
template <typename T>
using enable_if_not_copyable = std::enable_if_t<!std::is_copy_constructible<T>::value>;
template <typename T, typename Enabled=void>
struct Foo;
template <typename T>
struct Foo<T, enable_if_copyable<T>>
{
struct Bar
{
Bar();
};
};
template <typename T>
struct Foo<T, enable_if_not_copyable<T>> {
struct Bar
{
Bar();
};
};
template <>
struct Foo<void,void>
{
struct Bar
{
Bar();
//Bar() {} This compiles, but it is not what I want.
};
};
template <typename T>
Foo<T, enable_if_copyable<T>>::Bar::Bar()
{}
template <typename T>
Foo<T, enable_if_not_copyable<T>>::Bar::Bar()
{}
template <>
Foo<void, void>::Bar::Bar() // this does not compile
{}
int main() {
Foo<int>::Bar b;
Foo<void>::Bar v;
}
由于依赖关系,我必须实现Bar
的c'tors在他们的声明之外。
我的问题是所有编译器(Clang、gcc、Visual Studio 2015)都会提示 Foo<void, void>::Bar::Bar() {}
的实现class Foo<void, void>
声明之外。如果我实现Bar
的c'tor在 void
的特化内,我没有任何问题。
这是不可行的还是有人可以帮助我发现我的问题?
非常感谢!
最佳答案
尝试删除template<>
;我的意思是:
// template <>
Foo<void, void>::Bar::Bar() // now compile
{}
参见this page了解更多详情。
关于c++ - 如何在与定义分离的 void 上实现部分专用模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41287008/