C++ 原始代码使用boost::integral_常量作为下一层。基本上,就像你展示的那样。只是在我的样本中,我错误地使用它来快速给出一些显示错误的东西。不管怎样,经过你的解释,我现在明白得多了。今晚我将用更多的信息更新这个问题。@murre:很高兴它有意义。:
C++ 原始代码使用boost::integral_常量作为下一层。基本上,就像你展示的那样。只是在我的样本中,我错误地使用它来快速给出一些显示错误的东西。不管怎样,经过你的解释,我现在明白得多了。今晚我将用更多的信息更新这个问题。@murre:很高兴它有意义。:,c++,templates,sfinae,C++,Templates,Sfinae,原始代码使用boost::integral_常量作为下一层。基本上,就像你展示的那样。只是在我的样本中,我错误地使用它来快速给出一些显示错误的东西。不管怎样,经过你的解释,我现在明白得多了。今晚我将用更多的信息更新这个问题。@murre:很高兴它有意义。:)很高兴看到你仍在提供高质量的帮助(GMan:)@Georg:谢谢,无论如何都要努力。:) template <typename T> struct SomeClass { }; template <> struct
原始代码使用boost::integral_常量作为下一层。基本上,就像你展示的那样。只是在我的样本中,我错误地使用它来快速给出一些显示错误的东西。不管怎样,经过你的解释,我现在明白得多了。今晚我将用更多的信息更新这个问题。@murre:很高兴它有意义。:)很高兴看到你仍在提供高质量的帮助(GMan:)@Georg:谢谢,无论如何都要努力。:)
template <typename T>
struct SomeClass
{
};
template <>
struct SomeClass<char>
{
typedef char Type;
};
template <typename T>
struct IsChar
{
typedef char Yes;
typedef int No;
template <typename U>
static Yes Select(U*, typename SomeClass<U>::Type* p = 0);
template <typename U>
static No Select(U*, ...);
static T* MakeT();
const static bool Value = sizeof(Select(MakeT())) == sizeof(Yes);
};
if (IsChar<int>::Value)
{
...
if (IsChar<char>::Value)
{
...
typedef char Yes;
typedef int No;
template <typename U> static Yes Select(typename SomeClass<U>::Type* p);
template <typename U> static No Select(...);
static const bool Value = sizeof(Select<T>(0)) == sizeof(Yes);
template<class T1, class T2> struct SameType {
static const bool Value = false;
};
template<class T> struct SameType<T, T> {
static const bool Value = true;
};
template <typename T>
struct IsChar {
static const bool Value = SameType<T, char>::Value;
};
template<class T>
typename boost::enable_if_c<IsChar<T>::Value, void>::type
someFunction() {
}
template<class T>
typename boost::enable_if<boost::mpl::is_same<T, char>, void>::type
someFunction() {
}
template<class T> struct HasType {
template<class U> static char (&test(typename U::Type const*))[1];
template<class U> static char (&test(...))[2];
static const bool Value = (sizeof(test< SomeClass<T> >(0)) == 1);
};
// assume we have has_typedef_type from the Wikipedia page:
template <typename T>
void foo(const T& x)
{
if (has_typedef_type<T>::value)
{
// the predicate is true, so T::type exists
typename T::type y = x;
}
else
{
// the predicate is false, so T::type doesn't exist, do something else
float y = x;
}
}
// let's say we called it with double
void foo<double>(const double& x)
{
if (false)
{
// wait, double doesn't have this!
typename double::type y = x;
}
else
{
float y = x;
}
}
// in C++0x, these are defined in <type_traits>, but we'll do it ourselves
// (Boost has these as well)
typename <typename T, T Value>
struct integral_constant
{
typedef T type;
static const T value = Value;
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
namespace detail
{
// here are the real implementations
template <typename T>
void foo(const T& x, true_type)
{
// the predicate is true, so T::type exists
typename T::type y = x;
}
template <typename T>
void foo(const T& x, false_type)
{
// the predicate is false, so T::type doesn't exist, do something else
float y = x;
}
}
template <typename T>
void foo(const T& x)
{
detail::foo(x, // chose which function to call, using the type of this:
integral_constant<bool, has_typedef_type<T>::value>());
}