小编Vah*_*yan的帖子

明确的专长不能是朋友声明

代码

template <typename T>
void foo(const T& t)
{}

template <typename T>
class A
{
    template <>
    friend void foo<T>(const T& t)
    {}
};
Run Code Online (Sandbox Code Playgroud)

给出编译错误

"defining explicit specialization ‘foo<T>’ in friend declaration friend void foo<T>(const T& t)"
Run Code Online (Sandbox Code Playgroud)

用gcc和

"error C3637: 'A<int>::foo' : a friend function definition cannot be a specialization of a unction template"
Run Code Online (Sandbox Code Playgroud)

在VS2013中编译时

我了解该标准是这样说的,但是为什么呢?我想了解原因(在幕后),有很多文章写着“显式专业化不能成为朋友声明。”,但我不明白为什么。有任何想法吗?

c++ templates friend-function explicit-specialization

5
推荐指数
1
解决办法
1004
查看次数

const参数的模板参数推导

假设我们有

template <typename T>
void foo(T a)
{
    a = 10;
}

int main()
{
    const int a = 5;
    foo(a);
}
Run Code Online (Sandbox Code Playgroud)

为什么T被推断为int而不是const int,为什么我可以修改in函数?在这种情况下如何扣除?

这是一个工作样本.

c++ templates

3
推荐指数
1
解决办法
308
查看次数