如何在void上实现部分专用模板与定义分开

How to implement partial specialized template on void separate from definition?

本文关键字:定义 专用 void 实现部      更新时间:2023-10-16

我在具有部分专业的同时将内部类的实现分开时有问题。这是一个示例代码,可以说明我的问题:

#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'1。我的问题是所有编译器(Clang,GCC,Visual Studio 2015)都抱怨class Foo<void, void>声明之外的Foo<void, void>::Bar::Bar() {}实现。如果我在void上的专业化中实现Bar的C'4,我没有任何问题。这不是可行的还是可以帮助我发现问题的人?非常感谢!

尝试删除 template<>;我的意思是:

// template <>
Foo<void, void>::Bar::Bar() // now compile
{}

有关更多详细信息,请参见此页面。