我想要幾個重載的全局函數to_string()
採用某種類型T
並將其轉換爲其字符串表示形式。對於一般情況下,我希望能夠寫:使用SFINAE檢查全局運算符<<?
template<typename T,class OutputStringType> inline
typename enable_if<!std::is_pointer<T>::value
&& has_insertion_operator<T>::value,
void>::type
to_string(T const &t, OutputStringType *out) {
std::ostringstream o;
o << t;
*out = o.str();
}
我的has_insertion_operator
實施至今:
struct sfinae_base {
typedef char yes[1];
typedef char no[2];
};
template<typename T>
struct has_insertion_operator : sfinae_base {
template<typename U> static yes& test(U&);
template<typename U> static no& test(...);
static std::ostream &s;
static T const &t;
static bool const value = sizeof(test(s << t)) == sizeof(yes); // line 48
};
(它借用了this 和this) ,似乎工作。 但現在我希望有to_string
對於做不有operator<<
但做有自己to_string()
成員功能類型,即重載版本:
template<class T,class OutputStringType> inline
typename enable_if<!has_insertion_operator<T>::value
&& has_to_string<T,std::string (T::*)() const>::value,
void>::type
to_string(T const &t, OutputStringType *out) {
*out = t.to_string();
}
的has_to_string
實現是:
#define DECL_HAS_MEM_FN(FN_NAME) \
template<typename T,typename S> \
struct has_##FN_NAME : sfinae_base { \
template<typename SignatureType,SignatureType> struct type_check; \
template<class U> static yes& test(type_check<S,&U::FN_NAME>*); \
template<class U> static no& test(...); \
static bool const value = sizeof(test<T>(0)) == sizeof(yes); \
}
DECL_HAS_MEM_FN(to_string);
(這部分似乎工作正常,它改編自this。) 然而,當我有:
struct S {
string to_string() const {
return "42";
}
};
int main() {
string buf;
S s;
to_string(s, &buf); // line 104
}
我得到:
foo.cpp: In instantiation of ‘const bool has_insertion_operator<S>::value’:
foo.cpp:104: instantiated from here
foo.cpp:48: error: no match for ‘operator<<’ in ‘has_insertion_operator<S>::s << has_insertion_operator<S>::t’
好像SFINAE沒有發生。我如何正確書寫has_insertion_operator
以確定全球operator<<
是否可用?
僅供參考:我使用g ++ 4.2.1(在Mac OS X上作爲Xcode的一部分發布)。 另外,我希望代碼只能是沒有第三方庫的標準C++ 03,例如Boost。
謝謝!
這一切都是可行的,但*爲什麼*? – Potatoswatter 2011-04-24 03:49:56
@Patatoswatter:爲什麼不重要。請假定我的項目的其餘部分我知道我在做什麼。如果您必須知道,它是框架的一部分,用於傳遞任何類型的參數以形成本地化錯誤消息的一部分。這個問題沒有必要的所有細節。如果您知道如何操作,請回答問題。這將非常感激。 – 2011-04-24 03:53:40
爲什麼總是很重要。 – GManNickG 2011-04-24 06:47:37