考虑代码:
template <class T>
class Bar {
int foobar;
using X = T();
friend X foo;
};
void foo() {
Bar<void> bar;
bar.foobar = 1;
static_cast<void>(bar);
}
int main() {}
在 gcc 中编译都很好和 clang .但看似等效的代码:
template <class T>
class Bar {
int foobar;
friend T foo;
};
void foo() {
Bar<void()> bar;
bar.foobar = 1;
static_cast<void>(bar);
}
int main() {}
最佳答案
因为T foo
被解析为对象的声明,模板的实例化不能将对象的声明变为函数的声明。
C++ 标准/[temp.spec]:
If a function declaration acquired its function type through a dependent type (17.7.2.1) without using the syntactic form of a function declarator, the program is ill-formed.
关于c++ - 为什么我不能通过模板参数声明一种类型的友元函数但可以使用别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552534/