C++ 函数模板的函数作用域结构的静态成员函数是否被视为从属作用域?

C++ 函数模板的函数作用域结构的静态成员函数是否被视为从属作用域?,c++,C++,MSVC 9和g++-4.5不同意在下面的nested::baz中使用typename。哪个是正确的 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>::ty

MSVC 9和g++-4.5不同意在下面的
nested::baz
中使用
typename
。哪个是正确的

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;
}
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
$g++-4.5 test.cpp
test.cpp:在静态成员函数“static typename foo::type bar(T)::nested::baz(T)”中:
test.cpp:15:7:错误:需要在“foo::type”之前加上“typename”,因为“foo”是一个依赖作用域
test.cpp:15:20:错误:应为“;”在“结果”之前
test.cpp:16:14:错误:未在此作用域中声明“result”
test.cpp:在静态成员函数“static typename foo::type bar(T)::nested::baz(T)[带T=int,typename foo::type=int]”中:
test.cpp:20:23:从“typename foo::type bar(T)[with T=int,typename foo::type=int]实例化”
test.cpp:26:8:从此处实例化
test.cpp:15:7:错误:依赖名称“foo::type”被解析为非类型,但实例化会生成一个类型
test.cpp:15:7:注意:如果某个类型的意思是

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

MSVC似乎有错;见上文jagansai提供的相关问题:


以下是两位编译器都喜欢的
条码的解决方法:

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);
}
模板
typename foo::type
巴(T x)
{
typedef typename foo::type result\u type;
嵌套结构
{
内联静态结果_类型baz(T x)
{
结果型结果;
返回结果;
}
};
返回嵌套::baz(x);
}

GCC几乎肯定有正确的行为。如果从嵌套结构中删除typename,它将在vc11中编译。我现在只能访问它。“错误C2899:typename不能在模板声明之外使用”,这听起来不对。[模板之外的typename][1]请。请参阅此链接。[1] :@jagansai谢谢你的链接。这似乎是一个已知的问题。
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);
}