为什么我不能通过模板参数声明一种类型的友元函数但可以使用别名

Why can't I declare a type of friend function by template parameter but can with alias

考虑代码:

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 and 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() {}

导致两个 gcc and clang 都出错。为什么模板参数在这里不能等同于别名?

因为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.