rmn*_*rmn 3 c++ syntax templates friend
以下代码应该是自解释的.我有两个关于使用语法的问题(这是必须使用的语法).如果你能为这些提出的问题提供答案,我将永远感激不尽.
template <typename T>
struct A {
template <typename S>
void f (const A<S> &s);
template <typename S>
friend struct A;
// Question 1: why isn't the syntax 'friend struct A<S>' ?
// The semantic would stay, since we would like A<S> (for ANY S) to be friend of every A<T>..
private:
void g () const {}
};
template <typename T>
template <typename S> // Question 2: Why can't the syntax be 'template <typename T, typename S>' ?
void A<T>::f (const A<S> &s) {
s.g();
}
int main () {
A<bool> abool;
A<char> achar;
abool.f(achar);
}
Run Code Online (Sandbox Code Playgroud)
我已经确认这确实是唯一正确的语法(我很乐意发现我错了).我的问题更多的是关于语法背后的推理,正如问题的主体所解释的那样.
谢谢你的帮助.
为什么不是语法......
为什么语法不能......
你期望我们说什么?无论谁决定这种语法(主要是Stroustrup本人,AFAIK)都认为他们的语法比你的语法更好.
哪一个更好或更容易记住我不知道 - 但我确实发现它们比你的更有意义.当然,你可以自由地不同意.
编辑:好的,亚历山大很好地回答了问题#2.关于#1:
不同之处在于A<S>名称类型,这是函数参数所期望的类型,而A本身就是模板的名称,从中创建类型,如果您想要与模板而不是类型成为伙伴,这是有意义的:
template <typename S>
void f (const A<S> &s); // A<S> being the name of a type
template <typename S>
friend struct A; // A being the name of a template
Run Code Online (Sandbox Code Playgroud)
您可以与特定模板实例建立联系,而不是整个模板,但为此,模板必须已经由声明的编译器(即声明)知道friend:
template< typename T >
class foo;
class bar {
friend class foo<int>; // foo<int> being the name of a specific instance of foo
};
Run Code Online (Sandbox Code Playgroud)
所以交换模板是一个例外("通常" friend声明声明一个函数或类)并且确实需要不同的语法.
虽然我不能说为什么选择这种语法,但我可以说我会支持语言设计者做出的两个决定 - 它们对我有意义.在问题2中,您不仅有一个模板,还有两个嵌套的模板级别.为什么定义模板类的模板成员的语法会隐藏这个事实?这样,它只是现有模板语法的重新组合,而您的需要特殊规则将嵌套模板的模板参数合并为一个template<>.
| 归档时间: |
|
| 查看次数: |
385 次 |
| 最近记录: |