Re: using CLASS::type



Mycroft Holmes wrote:
template <typename T>
class my_base
{
public:
typedef T type;
};

template <typename T>
class my_object : public my_base<T>
{
public:
using typename my_base<T>::type;

type get_instance() const
{
return type();
}
};



class AAA
{
private:
class BBB
{
public:
int do_something() const { return 7; }
};

public:

int do_all() const
{ return my_object<BBB>().get_instance().do_something(); }
};



int main()
{
AAA myA;
return myA.do_all();
}




1) VC2008 returns error on "return type();"
error C2597: illegal reference to non-static member
'my_object<T>::type'

It is actually unclear whether the code is valid under the current
standard - see DR11:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#11

The latest draft of C++0x standard adds new wording that explicitly
allows this situation:

7.3.3p20 If a using-declaration uses the keyword typename and specifies
a dependent name (14.7.2), the name introduced by the using-declaration
is treated as a typedef-name (7.1.3).

But no such wording exists in C++98.

In any case, you can achieve the same goal with

typedef typename my_base<T>::type type;

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages


Loading