Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In using-declaration, can dependent names render to constructors after template substitution?

In this example:

template<class T>
struct S : T
{
    using T::X;
};

T::X is a dependent name that refers to the member X in T. If S<T> is instantiated with T = X:

struct X
{
    X(int) {}
};
...
S<X> s(42);

Will the using-declaration become inheriting-constructor?

Clang rejects the code DEMO, while g++ accepts it.

Note that if we write:

using T::X::X;

Both compilers accept the code and treat it as inheriting-constructor. Is using T::X allowed to become inheriting-constructor by standard?

like image 796
Jamboree Avatar asked Apr 07 '17 02:04

Jamboree


People also ask

What is the scope of a using declaration?

Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions, or to introduce enumerators into namespaces, block, and class scopes (since C++20).

What is the use of using declaration in c++?

A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .


1 Answers

Thanks to T.C. for pointing this out:

Core issue 2070, which is in the drafting stage (it's confirmed to be a problem, and the wording of the solution is being worked on), deals with this case. The proposed fix is requiring both IDs to be the same ID in order for this code to be accepted as an inheriting constructor.

In this light, the error message from clang makes sense, as T::X would be the type X, which triggers the "type from template without typename tag" error.

Original Post:

So it seems to me that the real question is, "Is it allowable that a template instantiation changes the semantic meaning of a using statement?"

And the answer is, it is not disallowed. I do not know if this interaction was foreseen and intended by the authors of the standard. But as far as I can see, referencing both the using declaration in Section 10 and the template initialization in Section 17, by the letter of the standard, yes, using T::X is allowed and yes, the using declaration will become an inheriting constructor when T is X.

like image 132
OmnipotentEntity Avatar answered Oct 26 '22 18:10

OmnipotentEntity