Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent name in local class

#include <type_traits>

template<typename... Ts>
void boo()
{
    struct A
    {
        struct B {};
    };
    static_assert(std::is_class_v<A>);
    static_assert(std::is_class_v<A::B>); // compilation failure; need typename.
}

int main()
{
    boo<int>();
}

Why is A::B a dependent name and A is not? Can not see anything in standard about that.

like image 298
Maksim S. Avatar asked Dec 04 '25 13:12

Maksim S.


1 Answers

This is CWG 2074, which is still unresolved [emphasis mine]:

2074. Type-dependence of local class of function template

According to 13.8.3.2 [temp.dep.type] paragraph 9, a local class in a function template is dependent if and only if it contains a subobject of a dependent type. However, given an example like

  template<typename T> void f() {
    struct X {
      typedef int type;
  #ifdef DEPENDENT
      T x;
  #endif
    };
  X::type y;    // #1
  }
  void g() { f<int>(); }

there is implementation variance in the treatment of #1, but whether or not DEPENDENT is defined appears to make no difference.

[...]

Perhaps the right answer is that the types should be dependent but a member of the current instantiation, permitting name lookup without typename.

Additional notes (September, 2022):

At present, the term "current instantiation" is defined for class templates only, and thus does not apply to function templates.

Moreover, the resolution for this issue should also handle local enums, with particular attention to 9.7.2 [enum.udecl] paragraph 1:

The elaborated-enum-specifier shall not name a dependent type and... This rule, without amendment, would disallow the following reasonable example if local enums were made dependent types:

template <class T>
void f() {
  enum class E { e1, e2 };
  using enum E;
}

The spec appears underspecified, as vendors seems to (on an implementation variance basis) enforce [temp.dep.type]/7.3 [emphasis mine]:

/7 A type is dependent if it is

  • [...]
  • /7.3 a nested class or enumeration that is a direct member of a class that is the current instantiation,

But as noted in CWG 2074, current instantiation as per [temp.dep.type]/1 does not cover what are actually the rules for the case of function templates.

like image 52
dfrib Avatar answered Dec 07 '25 03:12

dfrib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!