/************************************************************************/
/* Copyright (c) 2009, Roc King
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and other materials provided with the distribution.
3. Neither the name of the Tju nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/************************************************************************/
/**
這份代碼詳細(xì)介紹了使用SFINAE技術(shù)實(shí)現(xiàn)is_not_buildin_type的原理。
按順序由上往下閱讀即可。
*/
#include <stdio.h>
#include <iostream>
/** ------------------------------------------------------------------------ */
#define DEFINITION(prefix) \
prefix none {}; \
prefix data { int i; }; \
prefix function { void f(int ) {} }; \
prefix both { int i; double f() { return 0.0; } };
namespace structures { DEFINITION(struct) }
namespace s = structures;
namespace classes { DEFINITION(class) }
namespace c = classes;
namespace unions { DEFINITION(union) }
namespace u = unions;
#undef DEFINITION
/**
上面對(duì)class、struct、union分別定義了:
none 有數(shù)據(jù)成員,無(wú)成員函數(shù)
data 有數(shù)據(jù)成員,沒(méi)成員函數(shù)
function 無(wú)數(shù)據(jù)成員,有成員函數(shù)
both 有數(shù)據(jù)成員,有成員函數(shù)
*/
/** ------------------------------------------------------------------------ */
void test_pointer_to_data_member() {
// 一旦某個(gè)類型不是基本數(shù)據(jù)類型,就可以定義成員指針(數(shù)據(jù)成員指針,成員函數(shù)指針)。
// 即使它沒(méi)有數(shù)據(jù)成員或者成員函數(shù)。
// s::none 并沒(méi)有數(shù)據(jù)成員或者成員函數(shù)。
int s::none::* p; //但是可以定義一個(gè)指向s的數(shù)據(jù)成員指針,只要類型不是void
// void s::none::* p2; //error C2182: 'p2' : illegal use of type 'void'
// 同時(shí),在C++中,字面值0可以隱式轉(zhuǎn)換到任何指針類型。
p = 0; // ok
double s::none::* p3 = 0; // ok
// 但是,如果某類型沒(méi)有對(duì)應(yīng)類型的數(shù)據(jù)成員,就不能用數(shù)據(jù)成員指針去指向它。
int s::data::* p4 = 0;
p4 = &s::data::i; // ok
double s::data::* p5 = 0;
// p5 = &s::data::i;
// error C2440: '=' : cannot convert from 'int structures::data::* ' to 'double structures::data::* '
(void)p3; (void)p5;
}
// 這個(gè)是比較完整的測(cè)試
void test_pointer_to_data_member_integrate();
/** ------------------------------------------------------------------------ */
void test_pointer_to_member_function() {
// 同理,一旦某個(gè)類型不是基本類型,就可以定義指向該類型的成員函數(shù)的指針。
// 并且字面值0可以隱式轉(zhuǎn)換到該指針。
int (u::none::* p1)(void) = 0;
double (u::none::* p2)(double) = 0;
// 如果該類型確實(shí)有匹配的成員函數(shù),可以使用該成員函數(shù)給指針賦值。
double (u::both::* p3 )(void) = &u::both::f;
void (u::function::* p4)(int) = &u::function::f;
// 否則不能賦值
// double (u::both::* p5 )(void) = &u::function::f;
//error C2440: 'initializing' : cannot convert from 'void (__thiscall unions::function::* )(int)' to 'double (__thiscall unions::both::* )(void)'
// void (u::function::* p6)(int) = &u::both::f;
//error C2440: 'initializing' : cannot convert from 'double (__thiscall unions::both::* )(void)' to 'void (__thiscall unions::function::* )(int)'
(void)p1; (void)p2; (void)p3; (void)p4;
}
// 這個(gè)是比較完整的測(cè)試
void test_pointer_to_member_function_integrate();
/** ------------------------------------------------------------------------ */
/**
那么,測(cè)試一個(gè)類型是否是內(nèi)建類型的“一個(gè)”方法就是
*/
namespace SFINAE {
class true_type { char dummy; true_type(); };
class false_type { char dummy[2]; false_type(); };
// sizeof(true_type)!=sizeof(false_type)
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
template<typename T>
false_type is_not_buildin_type_test(
);
void test_theory() {
using namespace std;
/* 在當(dāng)前名字空間下, is_not_buildin_type_test是2個(gè)函數(shù)模板的名字:
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
(以下簡(jiǎn)稱模板1)
template<typename T>
false_type is_not_buildin_type_test(
);
(以下簡(jiǎn)稱模板2)
它們相互構(gòu)成重載。
*/
cout<<sizeof( is_not_buildin_type_test<c::none>(0) )<<endl;
// 0 可以隱式轉(zhuǎn)化成 int c::none::* ,可以匹配模板1 (with C = c::none)
// 這里int是無(wú)關(guān)緊要的, 只要不是void就行
// 當(dāng)然使用其他類型的數(shù)據(jù)成員指針 T C::* (T!=void)
// 或者成員函數(shù)指針進(jìn)行測(cè)試 T (C::*)(paramter_list),也是可以的
// 只是int C::* 寫起來(lái)比較方便。
// 因?yàn)?
) 可以匹配任何類型的對(duì)象,
// 所以 0 也可以匹配模板2
// 又因?yàn)?
)處于重載選擇優(yōu)先級(jí)中的最底層,所以最終匹配模板1。
// 注意,此處模板2不能使用(int),或者(T*)因?yàn)樗膬?yōu)先級(jí)高于(int C::*)
cout<<sizeof( is_not_buildin_type_test<double>(0) )<<endl;
// 0 不能隱式轉(zhuǎn)換成 int double::*,也就不能匹配模板1 ( with C=double )
// 但是還有一個(gè)“補(bǔ)救”的函數(shù)模板2,可以匹配任何類型的對(duì)象。
// 又因?yàn)镾FINAE(Substitution failure is not an error)機(jī)制
// 所以對(duì)模板1的失敗的匹配并不報(bào)錯(cuò)。
}
// 還有一些細(xì)節(jié)
// 比如is_not_buildin_type_test并沒(méi)有實(shí)現(xiàn)。
// 但是因?yàn)樗瑫r(shí)也沒(méi)有被調(diào)用, 而僅僅是用sizeof測(cè)試它的返回值,所以不算錯(cuò)誤。
// 也防止了客戶無(wú)意調(diào)用這個(gè)函數(shù)。
// 如何得知哪個(gè)is_not_buildin_type_test被重載選中?
// 是通過(guò)返回值的大小不同來(lái)區(qū)分的。
// 所以就需要true_type和false_type這2個(gè)東西。
// 其實(shí)更合理的命名應(yīng)該是small_type和big_type。
// 同時(shí),它們聲明有私有的構(gòu)造函數(shù),并且不實(shí)現(xiàn),防止客戶使用這2個(gè)類。
// 還因?yàn)閕s_not_buildin_type_test并沒(méi)有真正實(shí)現(xiàn)
// 也就沒(méi)有真正返回true_type或者false_type
// 所以沒(méi)有實(shí)現(xiàn)true_type和false_type的構(gòu)造函數(shù)也沒(méi)關(guān)系。
// 一切都因?yàn)閟izeof ……
/** -------------------------------------------------------------------- */
/**
將這種方法再包裝一下
(避免客戶去使用sizeof( xxx ) == sizeof( ture_type )等等)
就得到
*/
template<typename T>
class is_not_buildin_type {
is_not_buildin_type();
public:
enum { value =
sizeof(true_type)==sizeof( is_not_buildin_type_test<T>(0) ) };
};
// 或者將true_type,false_type,定義為它的內(nèi)嵌類型。
// 同時(shí)將is_not_buildin_type_test定義為它的靜態(tài)成員函數(shù)。
template<typename T>
class is_not_buildin_type2 {
is_not_buildin_type2();
// 因?yàn)槭莾?nèi)嵌的private,客戶不能訪問(wèn)
// 所以可以隨意一點(diǎn)
typedef char small_t;
struct big_t { small_t dummy[2]; };
template<typename U>
static big_t test(void (U::*)(short, float) );
// 只要是成員指針就ok,無(wú)論是數(shù)據(jù)成員指針還是成員函數(shù)指針。
// 也無(wú)論類型,簽名如何。
template<typename U>
static small_t test(
);
// 注意補(bǔ)救函數(shù)現(xiàn)在返回small_t
public:
// 但這也是無(wú)關(guān)緊要的,因?yàn)閟mall_t和big_t只是告之哪個(gè)重載被選中的方式。
// 只要這里處理好對(duì)應(yīng)就可以了。
enum { value= sizeof(big_t)==sizeof( test<T>(0) ) };
};
void test_wrapper() {
using namespace std;
cout<<is_not_buildin_type<c::data>::value<<endl;
cout<<is_not_buildin_type<u::both>::value<<endl;
cout<<is_not_buildin_type<float>::value<<endl;
cout<<is_not_buildin_type2<c::data>::value<<endl;
cout<<is_not_buildin_type2<u::both>::value<<endl;
cout<<is_not_buildin_type2<float>::value<<endl;
}
// 一個(gè)更完整的測(cè)試
void test_wrapper_integrate();
}
/** ------------------------------------------------------------------------ */
/**測(cè)試一個(gè)類型是否是內(nèi)建類型的另一個(gè)方法,需要更少的技巧。*/
namespace partial_specialization {
template<typename T>
struct is_not_buildin_type { enum { value=true }; };
// T不是一個(gè)內(nèi)建類型
// 除非
template<>
struct is_not_buildin_type<int> { enum { value=false}; };
// T是int
template<>
struct is_not_buildin_type<unsigned int> { enum { value=false}; };
// T是unsigned int
// .. more ..
}
int main()
{
using namespace std;
test_pointer_to_data_member();
test_pointer_to_data_member_integrate();
test_pointer_to_member_function();
test_pointer_to_member_function_integrate();
cout<<endl;
SFINAE::test_theory();
cout<<endl;
SFINAE::test_wrapper();
cout<<endl;
SFINAE::test_wrapper_integrate();
}
void test_pointer_to_data_member_integrate() {
// to do
}
void test_pointer_to_member_function_integrate() {
// to do
}
namespace SFINAE {
void test_wrapper_integrate() {
// to do
}
}
這個(gè)代碼已經(jīng)能很完美的解釋了~
今天看了C++ Templates 的15章.. 收獲頗多.. 以前的很多疑惑都比較開(kāi)朗了~
posted on 2009-03-16 23:32
Charlie 侯杰 閱讀(2659)
評(píng)論(3) 編輯 收藏 引用