/************************************************************************/
/* 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. */
/************************************************************************/
/**
這份代碼詳細介紹了使用SFINAE技術(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
/**
上面對class、struct、union分別定義了:
none 有數(shù)據(jù)成員,無成員函數(shù)
data 有數(shù)據(jù)成員,沒成員函數(shù)
function 無數(shù)據(jù)成員,有成員函數(shù)
both 有數(shù)據(jù)成員,有成員函數(shù)
*/
/** ------------------------------------------------------------------------ */
void test_pointer_to_data_member() {
// 一旦某個類型不是基本數(shù)據(jù)類型,就可以定義成員指針(數(shù)據(jù)成員指針,成員函數(shù)指針)。
// 即使它沒有數(shù)據(jù)成員或者成員函數(shù)。
// s::none 并沒有數(shù)據(jù)成員或者成員函數(shù)。
int s::none::* p; //但是可以定義一個指向s的數(shù)據(jù)成員指針,只要類型不是void
// void s::none::* p2; //error C2182: 'p2' : illegal use of type 'void'
// 同時,在C++中,字面值0可以隱式轉(zhuǎn)換到任何指針類型。
p = 0; // ok
double s::none::* p3 = 0; // ok
// 但是,如果某類型沒有對應類型的數(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;
}
// 這個是比較完整的測試
void test_pointer_to_data_member_integrate();
/** ------------------------------------------------------------------------ */
void test_pointer_to_member_function() {
// 同理,一旦某個類型不是基本類型,就可以定義指向該類型的成員函數(shù)的指針。
// 并且字面值0可以隱式轉(zhuǎn)換到該指針。
int (u::none::* p1)(void) = 0;
double (u::none::* p2)(double) = 0;
// 如果該類型確實有匹配的成員函數(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;
}
// 這個是比較完整的測試
void test_pointer_to_member_function_integrate();
/** ------------------------------------------------------------------------ */
/**
那么,測試一個類型是否是內(nèi)建類型的“一個”方法就是
*/
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;
/* 在當前名字空間下, is_not_buildin_type_test是2個函數(shù)模板的名字:
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
(以下簡稱模板1)
template<typename T>
false_type is_not_buildin_type_test(
);
(以下簡稱模板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是無關(guān)緊要的, 只要不是void就行
// 當然使用其他類型的數(shù)據(jù)成員指針 T C::* (T!=void)
// 或者成員函數(shù)指針進行測試 T (C::*)(paramter_list),也是可以的
// 只是int C::* 寫起來比較方便。
// 因為(
) 可以匹配任何類型的對象,
// 所以 0 也可以匹配模板2
// 又因為(
)處于重載選擇優(yōu)先級中的最底層,所以最終匹配模板1。
// 注意,此處模板2不能使用(int),或者(T*)因為它的優(yōu)先級高于(int C::*)
cout<<sizeof( is_not_buildin_type_test<double>(0) )<<endl;
// 0 不能隱式轉(zhuǎn)換成 int double::*,也就不能匹配模板1 ( with C=double )
// 但是還有一個“補救”的函數(shù)模板2,可以匹配任何類型的對象。
// 又因為SFINAE(Substitution failure is not an error)機制
// 所以對模板1的失敗的匹配并不報錯。
}
// 還有一些細節(jié)
// 比如is_not_buildin_type_test并沒有實現(xiàn)。
// 但是因為它同時也沒有被調(diào)用, 而僅僅是用sizeof測試它的返回值,所以不算錯誤。
// 也防止了客戶無意調(diào)用這個函數(shù)。
// 如何得知哪個is_not_buildin_type_test被重載選中?
// 是通過返回值的大小不同來區(qū)分的。
// 所以就需要true_type和false_type這2個東西。
// 其實更合理的命名應該是small_type和big_type。
// 同時,它們聲明有私有的構(gòu)造函數(shù),并且不實現(xiàn),防止客戶使用這2個類。
// 還因為is_not_buildin_type_test并沒有真正實現(xiàn)
// 也就沒有真正返回true_type或者false_type
// 所以沒有實現(xiàn)true_type和false_type的構(gòu)造函數(shù)也沒關(guān)系。
// 一切都因為sizeof ……
/** -------------------------------------------------------------------- */
/**
將這種方法再包裝一下
(避免客戶去使用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)嵌類型。
// 同時將is_not_buildin_type_test定義為它的靜態(tài)成員函數(shù)。
template<typename T>
class is_not_buildin_type2 {
is_not_buildin_type2();
// 因為是內(nèi)嵌的private,客戶不能訪問
// 所以可以隨意一點
typedef char small_t;
struct big_t { small_t dummy[2]; };
template<typename U>
static big_t test(void (U::*)(short, float) );
// 只要是成員指針就ok,無論是數(shù)據(jù)成員指針還是成員函數(shù)指針。
// 也無論類型,簽名如何。
template<typename U>
static small_t test(
);
// 注意補救函數(shù)現(xiàn)在返回small_t
public:
// 但這也是無關(guān)緊要的,因為small_t和big_t只是告之哪個重載被選中的方式。
// 只要這里處理好對應就可以了。
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;
}
// 一個更完整的測試
void test_wrapper_integrate();
}
/** ------------------------------------------------------------------------ */
/**測試一個類型是否是內(nèi)建類型的另一個方法,需要更少的技巧。*/
namespace partial_specialization {
template<typename T>
struct is_not_buildin_type { enum { value=true }; };
// T不是一個內(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
}
}
/* 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. */
/************************************************************************/
/**
這份代碼詳細介紹了使用SFINAE技術(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
/**
上面對class、struct、union分別定義了:
none 有數(shù)據(jù)成員,無成員函數(shù)
data 有數(shù)據(jù)成員,沒成員函數(shù)
function 無數(shù)據(jù)成員,有成員函數(shù)
both 有數(shù)據(jù)成員,有成員函數(shù)
*/
/** ------------------------------------------------------------------------ */
void test_pointer_to_data_member() {
// 一旦某個類型不是基本數(shù)據(jù)類型,就可以定義成員指針(數(shù)據(jù)成員指針,成員函數(shù)指針)。
// 即使它沒有數(shù)據(jù)成員或者成員函數(shù)。
// s::none 并沒有數(shù)據(jù)成員或者成員函數(shù)。
int s::none::* p; //但是可以定義一個指向s的數(shù)據(jù)成員指針,只要類型不是void
// void s::none::* p2; //error C2182: 'p2' : illegal use of type 'void'
// 同時,在C++中,字面值0可以隱式轉(zhuǎn)換到任何指針類型。
p = 0; // ok
double s::none::* p3 = 0; // ok
// 但是,如果某類型沒有對應類型的數(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;
}
// 這個是比較完整的測試
void test_pointer_to_data_member_integrate();
/** ------------------------------------------------------------------------ */
void test_pointer_to_member_function() {
// 同理,一旦某個類型不是基本類型,就可以定義指向該類型的成員函數(shù)的指針。
// 并且字面值0可以隱式轉(zhuǎn)換到該指針。
int (u::none::* p1)(void) = 0;
double (u::none::* p2)(double) = 0;
// 如果該類型確實有匹配的成員函數(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;
}
// 這個是比較完整的測試
void test_pointer_to_member_function_integrate();
/** ------------------------------------------------------------------------ */
/**
那么,測試一個類型是否是內(nèi)建類型的“一個”方法就是
*/
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;
/* 在當前名字空間下, is_not_buildin_type_test是2個函數(shù)模板的名字:
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
(以下簡稱模板1)
template<typename T>
false_type is_not_buildin_type_test(

(以下簡稱模板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是無關(guān)緊要的, 只要不是void就行
// 當然使用其他類型的數(shù)據(jù)成員指針 T C::* (T!=void)
// 或者成員函數(shù)指針進行測試 T (C::*)(paramter_list),也是可以的
// 只是int C::* 寫起來比較方便。
// 因為(

// 所以 0 也可以匹配模板2
// 又因為(

// 注意,此處模板2不能使用(int),或者(T*)因為它的優(yōu)先級高于(int C::*)
cout<<sizeof( is_not_buildin_type_test<double>(0) )<<endl;
// 0 不能隱式轉(zhuǎn)換成 int double::*,也就不能匹配模板1 ( with C=double )
// 但是還有一個“補救”的函數(shù)模板2,可以匹配任何類型的對象。
// 又因為SFINAE(Substitution failure is not an error)機制
// 所以對模板1的失敗的匹配并不報錯。
}
// 還有一些細節(jié)
// 比如is_not_buildin_type_test并沒有實現(xiàn)。
// 但是因為它同時也沒有被調(diào)用, 而僅僅是用sizeof測試它的返回值,所以不算錯誤。
// 也防止了客戶無意調(diào)用這個函數(shù)。
// 如何得知哪個is_not_buildin_type_test被重載選中?
// 是通過返回值的大小不同來區(qū)分的。
// 所以就需要true_type和false_type這2個東西。
// 其實更合理的命名應該是small_type和big_type。
// 同時,它們聲明有私有的構(gòu)造函數(shù),并且不實現(xiàn),防止客戶使用這2個類。
// 還因為is_not_buildin_type_test并沒有真正實現(xiàn)
// 也就沒有真正返回true_type或者false_type
// 所以沒有實現(xiàn)true_type和false_type的構(gòu)造函數(shù)也沒關(guān)系。
// 一切都因為sizeof ……
/** -------------------------------------------------------------------- */
/**
將這種方法再包裝一下
(避免客戶去使用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)嵌類型。
// 同時將is_not_buildin_type_test定義為它的靜態(tài)成員函數(shù)。
template<typename T>
class is_not_buildin_type2 {
is_not_buildin_type2();
// 因為是內(nèi)嵌的private,客戶不能訪問
// 所以可以隨意一點
typedef char small_t;
struct big_t { small_t dummy[2]; };
template<typename U>
static big_t test(void (U::*)(short, float) );
// 只要是成員指針就ok,無論是數(shù)據(jù)成員指針還是成員函數(shù)指針。
// 也無論類型,簽名如何。
template<typename U>
static small_t test(

// 注意補救函數(shù)現(xiàn)在返回small_t
public:
// 但這也是無關(guān)緊要的,因為small_t和big_t只是告之哪個重載被選中的方式。
// 只要這里處理好對應就可以了。
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;
}
// 一個更完整的測試
void test_wrapper_integrate();
}
/** ------------------------------------------------------------------------ */
/**測試一個類型是否是內(nèi)建類型的另一個方法,需要更少的技巧。*/
namespace partial_specialization {
template<typename T>
struct is_not_buildin_type { enum { value=true }; };
// T不是一個內(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
}
}
這個代碼已經(jīng)能很完美的解釋了~
今天看了C++ Templates 的15章.. 收獲頗多.. 以前的很多疑惑都比較開朗了~