MSVC 9 and g++-4.5 disagree over the use of typename
within nested::baz
below. Which is correct?
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;
}
MSVC's output:
$ 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
g++-4.5 issues no error, but if I remove the offending typename
, I receive an error message:
$ 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
Which is correct in this instance?
MSVC seems to be at fault; see this related question provided by jagansai above: typename outside of template
Here is a workaround for bar
which both compilers are happy with:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With