Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of implementing the auto keyword for C++03

Tags:

c++

In C++03 or earlier, is there a way of implementing the auto keyword? Not an object class, but so that it can be used like this [C++11]

auto x = 5;
std::cout << x;

I quickly 'whipped up' an implementation, but it is pretty rubbish, as you can cast it to any type - too much like an object class, and pretty basic, I know, but anyway, here it is:

class auto_t
{
public:
    template < typename _Ty > auto_t(const _Ty &_Value)
        : __data(_Value)
    {
    }

    template < typename _Ty >  operator _Ty()
    {
        return (_Ty)__data;
    }
private:
    void *__data;
};

#define auto auto_t
like image 782
Joseph Avatar asked Feb 24 '14 21:02

Joseph


1 Answers

Not really. That's why C++11 introduces it as a keyword, and not a library feature.

like image 167
Lightness Races in Orbit Avatar answered Sep 19 '22 19:09

Lightness Races in Orbit