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

W.F.

Consider the code:

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

Compiles fine in both gcc and clang. But seemingly equivalent code:

template <class T>
class Bar {
    int foobar;
    friend T foo;
};

void foo() {
    Bar<void()> bar;
    bar.foobar = 1;
    static_cast<void>(bar);
}

int main() {}

causes error in both gcc and clang. How come the template parameter doesn't work here equivalently to alias?

Oliv

Because T foo is parsed as the declaration of an object, and instantiation of a template cannot change a declaration of an object to a declaration of a function.

C++ standard/[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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I declare a trait function with tuple parameter matching?

Why can't I call a template friend function with explicit template arguments?

Why can't I specialize with a nullptr template parameter on a dependent type?

Why can't I pass a type member of a class as a template parameter?

Why can't a template type be a friend class in C++?

How can I make a typedef or alias for a template function that can also be used in friend function declarations?

Why I can declare a const reference using type-alias?

Why can't I declare a type with both template container and template contained value?

Why can't I specialize a template with an alias template in the return type in MSVC?

Why can't I call a generic function with a union type parameter?

Why can't I declare templated type aliases inside of functions?

How can I declare a template constant type?

Why can't the template type be inferred in this C++ function template?

Why can't I declare a pure virtual function with `= delete;`?

Why I can't declare void * p in the end of longestCommonPrefix function?

Why can't the compiler deduce template parameter from return type?

In a C++ function template, why can't I use a lambda to specify the array size of a parameter?

Can I declare an "alias" function which checks an object's type in TypeScript?

Why can you use just the alias to declare a enum and not the .NET type?

Why can I not pass a numeric template parameter to my templated function?

Why can't I use QList::size_type as I would std::string::size_type? (template parameter error)

Why can't I return an instance of template parameter?

Why can't I wrap a template parameter with parentheses?

Can we declare a friend function with no argument?

How can I declare a function parameter that can be a string or a function, in Kotlin?

Can I declare a function using a type

Why function can't accept array alias

Why can't a PRIVATE member function be a friend function of another class?

Why Dll can't export a function with parameter type FILE*