gpt4 book ai didi

c++ - 如何在与定义分开的 void 上实现部分专用模板?

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:29 25 4
gpt4 key购买 nike

我在具有部分特化的同时分离内部类的实现时遇到了问题。这是说明我的问题的示例代码:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com