Thread: Cannot add const to references?

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Cannot add const to references?

    I'm having problems, and I'm wondering if this is normal or nor and if there's some workaround.
    Consider:

    Code:
    namespace mpl = Stuff::mpl;
    
    template<typename T>
    struct foo { typedef const T type; };
    
    int main()
    {
    	A a;
    	a.x = 0;
    	cout << mpl::type_name<foo<A&>::type>::name << endl;
    }
    mpl::type_name is a meta-template struct helper that helps in printing out names of the types since Microsoft's typeid.name() ignores const and reference identifiers on the types.
    Regardless of that, this does not work as expected. You would expect it to return (or I would) const A&, but it doesn't. It's still the same old A&.

    Visual Studio warns me:
    Warning 1 warning C4181: qualifier applied to reference type; ignored g:\w00t\visual studio 2008\projects\temp\temp2.cpp 35
    Which essentially means I cannot append const to a reference type.

    So is there a way to add const to a reference type?
    The idea is to do:

    void foo(typename mpl::ArgumentHelper<T>::const_type Arg)

    And const_type will be T if it's a native type and const T& if it's a class or struct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, what you're experiencing is "normal", or at least ... expected.

    I may not have fully understood your intent, but my guess is that you're looking for something like this;
    Code:
    template<typename T> class MyTraitsClass
    {
         typedef const T & const_type;
    };
    
    template<> class MyTraitsClass<short>
    {
        typedef short const_type;
    };
    
    // other specialisations for the other basic types.
    
    template<typename T>  class TypeStripper
    {
          typedef typename T actual_type;
    };
    
    // and a series of partial specialisations for reference types
    
    template<typename T &>  class TypeStripper
    {
          typedef typename T actual_type;
    };
    
    template<typename const T &>  class TypeStripper
    {
          typedef typename T actual_type;
    };
    
    template<typename T> class ArgumentHelper
    {
        typedef typename MyTraitsClass<TypeStripper<T>::actual_type>::const_type const_type; 
           // may need typename keyword more than once in here:  check that
    };

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Strip the reference, add const and re-add the reference!
    Works like a charm! Thanks!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. vector<>
    By teval in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2003, 03:27 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM