Do decltype need an expression that includes the full type?

Consider the following code that tries to determine the existence of a nested typedef.

  #include<type_traits>
  struct foo;// incomplete type
  template<class T> 
  struct seq
  {
       using value_type = T;
  };
  struct no_type{};
  template<class T>
  struct check_type : std::true_type{};
  template<> 
  struct check_type<no_type> :std::false_type{};
  template<class T> 
  struct has_value_type
  {
    template<class U>
    static auto check(U const&)-> typename U:: value_type;
     static auto check(...)->no_type;  
    static bool const value = check_type<decltype(check(std::declval<T>()))>::value;
    using type = has_value_type;
  };
  int main()
  {
      char c[has_value_type<seq<foo>>::value?1:-1];
      (void)c;
  }

The call has_value_type<seq>::valuecauses a compilation error as an invalid use of an incomplete type seq<foo>::value_type. decltypeneed a full type of expression? If not, how to remove the error? I am using gcc 4.7 to compile.

+3
source share
2 answers

Your code is valid. C ++ 11, which defines that a call to a top-level function that appears as a decltype operand, does not introduce a temporary, even when the call is prvalue.

, ( ), .

+3

decltype , , , , .

template<class U>
auto check(U const&) -> typename U::value_type;

foo, U seq<foo>. , . , . void_<typename U::value_type> ( template<typename T> struct void_ {};), , , .

+2

All Articles