Introduction to C++ 11 Series – Part 3, Local & Unnamed Types as Template Arguments

March 23, 2013

In this post, I am going to write about a new feature, that your response might be “Ha! I didn’t know I can do that”.

The old C++ standard says that local types, unnamed types and in general, types with no linkage, shall not be used as template arguments. In C++ 11 this has been solved:

  • local types have the same linkage as their enclosing function
  • Unnamed types have the same linkage they would have if they were named

To show the change look at this code:

 

template <class T> void typeid_of(T t)

{

       cout << "Typeid: " << typeid(t).name() << endl;

}

 

void local_unamed_types()

{

       struct Local {};

       enum { No = 0, Yes = 1 };

 

       typeid_of(Local());

       typeid_of(Yes);

}

 

This code needs Visual Studio 2010 C++ compiler and above to be compiled.

The result:

 

clip_image001[6]

 

You can see that the “un named” types are under the scope of the local_unamed_types function.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. GraliaExhauraMay 2, 2013 ב 17:57

    Spot on with this write-up, I truly believe this web page requirements a lot more consideration. I’ll most likely be again to read far more, thanks for that info.

    [url=http://www.michaelkorsbagsnew.com]michael kors hand bags[/url]

    Reply
  2. nipreineeMay 9, 2013 ב 14:44

    Spot on with this write-up, I really feel this website requirements a lot more consideration. I’ll probably be again to read much more, thanks for that information.

    [url=http://outletredsoleshoesc.zitecode.com/]christian louboutin discount[/url]

    Reply