Code
#include <iostream>
using namespace std;
#define PF cout << __PRETTY_FUNCTION__ << endl;
class berlp {
public:
berlp() { }
void p() { }
};
template <typename T>
class derp {
public:
derp() = default;
derp(const T & a) : mem(a) {
a.p();
mem.p();
PF
}
template <typename U>
derp(U && a) : mem(std::forward<U>(a)) {
PF
}
T mem;
};
int main(int argc, const char * argv[])
{
berlp one;
derp<berlp &> f(one); // problems with this list below
derp<const berlp &> h(one); // problem with this follows
return 0;
}
Output Using XCode and CLang This all compiles fine, and here is the output...
derp<berlp &>::derp(const T &) [T = berlp &]
derp<const berlp &>::derp(U &&) [T = const berlp &, U = berlp &]
Problems
derp<berlp &> f(one);
: a.p() in the derp constructor should fail since "a" is "const berlp &" after reference collapsing, and p() is not const. Secondly, initialization of "mem" (berlp &) with "a" (const berlp &) should not work. It seems like the "const" in "derp(const T & a)" is just not doing anything. Finally, why does it even use the first constructor and not the templated one which seems like it would do all of this without breaking the const?
derp<const berlp &> h(one);
: why does this call use the templated constructor when the other seems to be exactly what we are after? This isn't too terrible a problem, since it doesn't seem to break anything, but it does let you modify the passed berlp in the constructor, while the other constructor (supposedly) shouldn't.
So, I'm either terribly confused, or something is up! Please help!