0

I'm new to BOOST_AUTO and here is a basic question.

I defined my own class and used it with BOOST_AUTO. According to the tutorial:

If your define your own type, the Typeof Library cannot handle it unless you let it know about this type. You tell the Typeof Library about a type (or template) by the means of "registering" this type/template.

But my code below doesn't register anything and runs correctly in VC++2010. Is this registration really necessary. Did I do anything wrong?

#include <boost/typeof/typeof.hpp>
using namespace std;
namespace NS1 {
    struct X {
        X(const X& x){s=x.s+1;}
        X(){s=3;}
        int s;
    };
}
int _tmain(int argc, _TCHAR* argv[])
{
    NS1::X x;
    BOOST_AUTO(y,x);

    cout << y.s << endl;
    return 0;
}

output:4

JavaMan
  • 4,954
  • 4
  • 41
  • 69
  • It's possible your compiler supports regular `auto`. – chris May 21 '16 at 13:11
  • oh yes it does! https://msdn.microsoft.com/en-us/library/dd293615(v=vs.100).aspx. Does that mean BOOST_AUTO automatically #define to `auto` in VC++2010? – JavaMan May 21 '16 at 13:17
  • 1
    @JavaMan: It doesn't matter, just use regular `auto`. – John Zwinck May 21 '16 at 13:20
  • @JavaMan, From what I've seen of Boost, most libraries like this one are pretty good at using a newer feature directly if the compiler supports it. – chris May 21 '16 at 15:41

1 Answers1

0

from the docs:

The BOOST_AUTO macro emulates the proposed auto keyword in C++.

looking into the code it seems they replace BOOST_AUTO to BOOST_TYPEOF.

typeof seems to be the old extension name of decltype in GCC compilers. (See this SO question). The code of the boost typeof library doesn't use the auto keyword apparantly for msvc (See here).

It seems that at the time of writing this library, there were some compilers who did support the auto keyword while others did not. If you want to compile the code on architectures that don't support this feature, the registration is necessary.

In any case, since C++11 is released for a while by now. Using this library if you compiler already supports the auto keyword should be considered bad practise, because this library only exists to emulate the proposed feature.

Except of course you have to compile to C++03 for some reason.

Community
  • 1
  • 1
Alexander Oh
  • 24,223
  • 14
  • 73
  • 76