I’m working through “C++ Template Metaprogramming” by Abrahams & Gurtovoy”
This isn’t actually in chapter two but is something I tried whilst working on the first exercise (2.10, 2.0) which is confusing me:
#include <iostream>
#include <boost/type_traits.hpp>
std::string display(bool b)
{
return (b ? "true" : "false");
}
int main()
{
using namespace std;
cout << display(boost::is_same<int const&, boost::add_const<int &>::type >::value) << "\n";
return 0;
}
The output is ‘false’.
However if I remove the references, i.e. ‘int const’ and ‘int’. The output is ‘true’.
If you tried the same thing with pointers, as in
you’d discover that it is also false, since
boost::add_const<int *>::typegeneratesint *consttype, which is obviously not the same asint const *.Essentially the same thing happens with references, i.e.
boost::add_const<int &>::typeis an attempt to generateint &const. Formally, typeint &constis illegal in C++ – cv-qualification cannot be applied to the reference itself. So,boost::add_constis designed to be a no-op in this case, meaning thatboost::add_const<int &>::typegeneratesint &again.