Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2010-05-26 17:26:15


[Tomas Puverle]
> You cannot overload based on const vs non-const ref

Of course you can.

C:\Temp>type meow.cpp
#include <iostream>
#include <ostream>
#include <string>
using namespace std;

template <typename T> void meow(T&) {
    cout << "modifiable!" << endl;
}

template <typename T> T meow(const T& t) {
    cout << "const!" << endl;
    return t;
}

string modifiable_rvalue() {
    return "purr";
}

// For demonstration purposes only.
// Functions shouldn't return by const value.
const string const_rvalue() {
    return "hiss";
}

int main() {
    int m = 1701;
    const int c = 1729;

    meow(m);
    meow(c);

    string x = "cat";
    const string y = "kitty";

    meow(x);
    meow(y);
    meow(modifiable_rvalue());
    meow(const_rvalue());
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp

C:\Temp>meow
modifiable!
const!
modifiable!
const!
const!
const!

Stephan T. Lavavej
Visual C++ Libraries Developer


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk