struct A { virtual ~A() {} };
struct B : A {};
B b;
A* a_ptr = &b;B* b_ptr = polymorphic_downcast (a_ptr);A& a_ref = b;
B& b_ref = polymorphic_downcast (a_ref);
这个题没有理解题意,不会做, ~~~~~~
2-3. 使用type traits
设施实现一个type_descriptor类模板,当被流化(streamed)时,其实例打印
其模板参数的类型:
std::cout<
std::cout<
你可以假定type_descriptor的模板参数局限于
根据以下四种整型构建的复合类型:char、short int、int以及long int。
这个题比较简单,不说了直接贴代码:
template
struct get_description
{
static std::string value;
operator const char*()
{
return value.c_str();
}
};
template
std::string get_description
::value = "can not deduce the type"; template<> std::string get_description
::value = "int"; template<> std::string get_description
::value = "char"; template<> std::string get_description
::value = "short"; template<> std::string get_description
::value = "long"; template<> std::string get_description
::value = "float"; template<> std::string get_description
::value = "double"; template
struct get_description
{ operator const char*() { static std::string ret = get_description
(); ret += " const"; return ret.c_str(); } }; template
struct get_description
{ operator const char*() { static std::string ret = get_description
(); ret += " *"; return ret.c_str(); } }; template
struct get_description
{ operator const char* () { static std::string ret = get_description
(); ret += " &"; return ret.c_str(); } };
测试代码:
void fun_type_descriptor()
{
std::cout << get_description
() << std::endl;
std::cout << get_description
()<< std::endl; //std::cout << get_description
()<< std::endl; }
模板,使其输出type的伪英语描述,就像cdecl程序的
explain命令所做的那样:
//打印“array of pointer to function returning pointer to char”
std::cout << type_descriptor< char *(*[])() >();
这个题也比较简单,直接上代码:
template
struct get_description
{
static std::string value;
operator const char*()
{
return value.c_str();
}
};
template
std::string get_description
::value = "can not deduce the type"; template<> std::string get_description
::value = "int"; template<> std::string get_description
::value = "char"; template<> std::string get_description
::value = "short"; template<> std::string get_description
::value = "long"; template<> std::string get_description
::value = "float"; template<> std::string get_description
::value = "double"; template
struct get_description
{ operator const char*() { static std::string ret = get_description
(); ret += " const"; return ret.c_str(); } }; template
struct get_description
{ operator const char*() { static std::string ret = get_description
(); ret += " volatile"; return ret.c_str(); } }; template
struct get_description
{ operator const char*() { static std::string ret = "pointer to "; ret += get_description
(); return ret.c_str(); } }; template
struct get_description
{ operator const char* () { static std::string ret = "reference to "; ret += get_description
(); return ret.c_str(); } }; template
struct get_description
{ operator const char* () { std::cout << typeid(T).name() << "\n"; static std::string ret = "array of "; ret += get_description
(); std::cout << typeid(T).name() << "\n"; return ret.c_str(); } }; template
struct get_description
{ operator const char* () { std::cout << typeid(T).name() << "\n"; static std::string ret = "array of "; ret += get_description
(); std::cout << typeid(T).name() << "\n"; return ret.c_str(); } }; template
struct get_description
{ operator const char* () { static std::string ret = "pointer to function returning "; ret += get_description
(); return ret.c_str(); } }; template
struct get_description
{ operator const char* () { static std::string ret = "this is a pointer function wit