我可以专门研究向前声明的模板吗?

问题描述

我可以专门研究正向声明的模板吗?例如:

template <typename T> class A;

template <>
class A<char> {
    char a[1000];
};

int main()
{
    [[maybe_unused]] A<char> a;
    return 0;
}

我想实现什么?

我们知道,我们必须专门std::hash才能与基于哈希表的某些类型一起使用。标准的std::hash专业化要求在头文件中包含<functional>,然后对其进行专业化。我在许多地方都使用了此头文件,<functional>的编译时间非常长。因此,我想将我的专长移到源(cpp)文件。

my_type.hpp:

class my_type {/*...*/};

namespace std {

template <typename T>
struct hash;

template <>
struct hash<my_type>
{
    size_t operator()(my_type m) const;
};
} // namespace std

my_type.cpp:

#include "my_type.hpp"
#include <functional>
namespace std {
size_t std::hash<my_type>::operator()(my_type v) const
{
    return std::hash<decltype(v.value())>{}(v.value());
}
} // namespace std

此解决方案有效,但是按照ISO标准合法吗?

编辑/注意::它不适用于libc ++(clang std实现),因为它将std::hash定义为std::__1::hash,其中__1是内联的命名空间。这部分地回答了这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)