函数模板的函数范围结构的静态成员函数是否被视为依赖范围?

Jar*_*ock 5 c++

MSVC 9和g ++ - 4.5不同意对使用typenamenested::baz的下方.哪个是对的?

template<typename T>
  struct foo
{
  typedef T type;
};

template<typename T>
  typename foo<T>::type
    bar(T x)
{
  struct nested
  {
    inline static typename foo<T>::type baz(T x)
    {
      typename foo<T>::type result;
      return result;
    }
  };

  return nested::baz(x);
}

int main()
{
  int x;
  bar(x);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

MSVC的输出:

$ cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
test.cpp(15) : error C2899: typename cannot be used outside a template declaration
Run Code Online (Sandbox Code Playgroud)

g ++ - 4.5没有发出错误,但是如果我删除了违规行为typename,我会收到一条错误消息:

$ g++-4.5 test.cpp
test.cpp: In static member function 'static typename foo<T>::type bar(T)::nested::baz(T)':
test.cpp:15:7: error: need 'typename' before 'foo<T>::type' because 'foo<T>' is a dependent scope
test.cpp:15:20: error: expected ';' before 'result'
test.cpp:16:14: error: 'result' was not declared in this scope
test.cpp: In static member function 'static typename foo<T>::type bar(T)::nested::baz(T) [with T = int, typename foo<T>::type = int]':
test.cpp:20:23:   instantiated from 'typename foo<T>::type bar(T) [with T = int, typename foo<T>::type = int]'
test.cpp:26:8:   instantiated from here
test.cpp:15:7: error: dependent-name 'foo<T>::type' is parsed as a non-type, but instantiation yields a type
test.cpp:15:7: note: say 'typename foo<T>::type' if a type is meant
Run Code Online (Sandbox Code Playgroud)

在这种情况下哪个是正确的?

Jar*_*ock 2

MSVC 似乎有问题;请参阅上面 jagansai 提供的相关问题:模板之外的类型名


bar这是两个编译器都满意的解决方法:

template<typename T>
  typename foo<T>::type
    bar(T x)
{
  typedef typename foo<T>::type result_type;

  struct nested
  {
    inline static result_type baz(T x)
    {
      result_type result;
      return result;
    }
  };

  return nested::baz(x);
}
Run Code Online (Sandbox Code Playgroud)