https://godbolt.org/z/s5Yh8e6b8
我不明白这背后的原因:为什么使用私有类型的显式专门化允许类模板,而不允许函数模板?
假设我们有一堂课:
class pepe
{
struct lolo
{
std::string name = "lolo";
};
public:
static lolo get()
{
return {};
}
};spec_class<pepe::lolo>{},因为pepe::lolo是不可访问的。template <typename>
struct spec_class
{};
// this is ok
template <>
struct spec_class<pepe::lolo>
{};
// this will be ok also upon implicit instantiation
template <typename T>
void template_func(const T &t)
{
std::cout << "implicit: " << t.name << std::endl;
}但是:
// this is not ok!
// template <>
// void template_func(const pepe::lolo &p)
// {
// std::cout << "explicit: " << p.name << std::endl;
// }
// this is ok, but more or less understandable why
template <>
void template_func(const decltype(pepe::get()) &p)
{
std::cout << "explicit: " << p.name << std::endl;
}
// not ok, but expected
// void func(const pepe::lolo&)
// {}那么:为什么禁止函数模板的显式专门化?
发布于 2022-05-15 22:06:49
在C++20中,由于PR0692,在函数模板的专门化参数中使用私有成员是完全有效的。特别是,在temp.spec.general#6中添加了以下措辞
通常的访问检查规则不适用于显式实例化或显式专门化的声明中的名称,但出现在函数体、默认参数、基子句、成员规范、枚举器列表或静态数据成员或变量模板初始化器中的名称除外。 注意1:特别是,函数声明器中使用的模板参数和名称(包括参数类型、返回类型和异常规范)可以是通常无法访问的私有类型或对象。-尾注
(强调地雷)
https://stackoverflow.com/questions/72251287
复制相似问题