Re: Reference Type
On Mar 26, 4:02 am, Juha Nieminen <nos...@thanks.invalid> wrote:
So is there any situation anywhere where you can distinguish between =
a
value and a const reference of the same type?
#include <iostream>
#include <boost/type_traits.hpp>
template <class T> struct get_name;
template <>
struct get_name<int>
{
std::string operator()() const {return "int";}
};
template <>
struct get_name<char>
{
std::string operator()() const {return "char";}
};
template <class T>
void
display()
{
using namespace boost;
typedef typename remove_reference<T>::type Tr;
typedef typename remove_cv<Tr>::type Trcv;
if (is_const<Tr>::value)
std::cout << "const ";
if (is_volatile<Tr>::value)
std::cout << "volatile ";
std::cout << get_name<Trcv>()();
if (is_reference<T>::value)
std::cout << '&';
std::cout << '\n';
}
int main()
{
display<int>();
display<int&>();
display<const int&>();
display<char>();
display<char&>();
display<const char&>();
}
Outputs for me:
int
int&
const int&
char
char&
const char&
The display function above can be refined/extended to portably print
out a good description of any type.
If it helps, here is a diagram of C++ types and how they are
classified (those types you may not recognize are introduced in C+
+0X):
http://home.roadrunner.com/~hinnant/TypeHiearchy.pdf
The <boost/type_traits.hpp> used above will be <type_traits> in
namespace std for C++0X (well, very similar, not exactly the same).
-Howard