Decltype type and member (not pointer)
struct C
{
int Foo(int i) { return i; }
typedef decltype(C::Foo) type;
};
Since there is no such type as a member function type (no, is there?), I expect there to C::type
be int (int)
.
But the following won't compile using Visual C ++ 2012 RC:
std::function<C::type> f;
So which type decltype(C::Foo)
?
source to share
The code is ill-formed: there are only a few ways to use the name of a member function (for example C::Foo
), and this is not one of them (a full list of valid uses can be found in the C ++ language, see C ++ 11 ยง5.1.1 / 12 ).
In the context of your example, the only thing you can really do is take the address of a member function &C::Foo
to form a pointer to a member function of the type int (C::*)(int)
.
Since the code is poorly formed, the compiler should reject it. In addition, it gives inconsistent results depending on how it is used C::Foo
; we'll look at the inconsistency below.
Report the bug to Microsoft Connect . Alternatively, let me know and I am happy to report it.
If you have a type but do not know what a type is, you can find out the name of that type by using it so that the compiler will throw an error. For example, declare a class template and never define it:
template <typename T>
struct tell_me_the_type;
Then you can instantiate this template with the type you are interested in:
tell_me_the_type<decltype(C::Foo)> x;
Since it is tell_me_the_type
not defined, the definition x
is incorrect. The compiler must include the type T
in the error it emits. Visual C ++ 2012 RC reports:
error C2079: 'x' uses undefined struct 'tell_me_the_type_name<T>'
with
[
T=int (int)
]
The compiler assumes it C::Foo
has a type int (int)
. If so, then the compiler should accept the following code:
template <typename T>
struct is_the_type_right;
template <>
struct is_the_type_right<int(int)> { };
is_the_type_right<decltype(C::Foo)> x;
The compiler does not accept this code. It reports the following error:
error C2079: 'x' uses undefined struct 'is_the_type_right<T>'
with
[
T=int (int)
]
So C::Foo
both are of type int (int)
and non-type int (int)
, which violates the consistency principle . :-)
source to share