Templates, typename, lambda & # 8594; are the dependent names independent?

Consider:

template < typename Something >
boost::function<void()> f()
{
  typedef typename Something::what type;
  return [](){};
}

In this code, you need a type name because “what” is a dependent name. But consider the following:

template < typename Something >
boost::function<void()> f()
{
  return []()
  { 
    typedef typename Something::what type;
  };
}

Compiler Bitches: "typename cannot be used outside the template declaration"

WTF?

It works:

template < typename Something >
boost::function<void()> f()
{
  return []()
  { 
    typedef Something::what type;
  };
}

What happens with lambda creation, which means “what” is not a dependent name? Or is it just a mistake?

Heh ... correction. The latter does not work. It says that "Something" does not exist. This modified version works, although it is still not intuitively needed and will not accept "typename".

template < typename T > struct wtf { typedef typename T::what type; };

template < typename Something >
boost::function<void()> f()
{
  return []() { typedef wtf<Something>::type type; };
}

Of course, now I have TWO QUESTIONS: the original and WTF does not find "something" if it is not used as a template parameter

+3
2

. , "WTF" ( typename -) N3225 5.1.2/7:

- , , , id- , (* this), -.

Something -, - .

+3

, , , , , typename .

0

Source: https://habr.com/ru/post/1783304/


All Articles