roger.james
roger.james

Reputation: 1498

Why the error in this simple template code?

Why does the error occur below?

#include <type_traits>

template<typename FooType>
struct bar {
  using bar_type = typename FooType::foo_type;
};

template<typename T>
struct foo {
  using foo_type = T;

  //Error: No type named 'bar_type' in 'bar<foo<int> >'
  static_assert(std::is_same<foo_type,typename bar<foo<T>>::bar_type>::value,"");
};

int main(int argc, char **argv)
{
  bar<foo<int>> b;
  return 0;
}

Upvotes: 2

Views: 88

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 71899

At the point of the static_assert, foo isn't yet a complete type (you're still within its definition), so when bar tries to reach in, the compiler gives you an error. The particular error it gives you sucks though; try it with a different compiler.

Upvotes: 5

Related Questions