const references initialized to a literal

This is a discussion on const references initialized to a literal within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm having an hard time understanding both the usefulness and the reason why I am allowed to write ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439

    const references initialized to a literal

    Hi all,

    I'm having an hard time understanding both the usefulness and the reason why I am allowed to write something as:
    Code:
    const int &ref = 12;
    I understand I couldn't do this if I hadn't declared the reference as const. I also understand that the compiler transforms that code into something like:
    Code:
    const int temp = 12; //create a temporary object
    const int &ref = temp; //bing the reference to the temporary object;
    However, I cannot conceive a single use for this const reference initialized to a literal. Is there any?

    Another problem is why am I allowed to intialize to a literal? I can understand why I am not on the case of non const references, but why am I on const references? Even on the highest warning level for C++ 2005 Express and using -Wall and -Wextra on Dev-Cpp I get no single warning.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    I think it would be for cases like:
    Code:
    void foo(const T& t = 0) {
    	// ...
    }
    
    // ...
    foo(1);
    where (const) int (or whatever literal type in question) is convertible to some type T.

    Another problem is why am I allowed to intialize to a literal? I can understand why I am not on the case of non const references, but why am I on const references?
    I do not see why not. The const reference means that one will not be assigning to an rvalue.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Maybe i'm fixing myself too much on the reference definition, which states references cannot be assigned to diferent types. As such I cannot assign a double reference to an int type. It's bugging me that I can do exactly that by assigning an int const reference to a literal type.

    So, probably, what this means is that constant references can be assigned to related types?

    The following code worked, when I tested it:

    Code:
    int ival = 10;
    const int &ref = ival;
    const double &ref2 = ival;
    
    cout << ival << " " << ref << " " << ref2 << endl
    Your initial example, laserlight... you mean it is useful for function templates? I'm still green here

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Maybe i'm fixing myself too much on the reference definition, which states references cannot be assigned to diferent types.
    hmm... I do not recall such a rule. My reasoning is that a reference is effectively an alias for what it refers to, so if what the reference refers to is convertible to another type, the reference too should be convertible to that type.

    I think what you mean to say is that one cannot assign to a reference of a different type (but note the exception where polymorphism comes into play).

    The contrast would then be between:
    Code:
    int main() {
    	double a = 0.0;
    	int& b = static_cast<int>(a);
    }
    which produces a compile error, and
    Code:
    int main() {
    	double a = 0.0;
    	const int& b = static_cast<int>(a);
    }
    which doesnt. In the latter my opinion is that it is safe for the rvalue to be passed to const reference, since the const-ness means the rvalue wont be changed.

    As such I cannot assign a double reference to an int type.
    You can assign a double (reference or not) to an int, but with possible loss of data.

    Your initial example, laserlight... you mean it is useful for function templates?
    Um, my example is a non-template function. My use of 'T' is just laziness since it can be any appropriate type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  2. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21