我已经有SFINAE代码来检测给定的成员函数是否存在并且可以接受特定类型的实例.
我试图检测何时有一个特定的成员函数,因为它的参数是非参考.
示例代码
template<typename TYPE_T>
class HasSwapMemberImpl
{
/*
* This uses the compilers type deduction magic to ensure that there's SOME swap function
* that's a member of CLASS_T, that can be passed an instance of CLASS_T, in some fashion.
* This doesn't determine that the argument for the function is a reference.
*/
template <typename CLASS_T>
static auto test_compatible_swap_function_exists(CLASS_T * p) -> decltype(p->swap(*static_cast<CLASS_T*>(nullptr)), boost::true_type());
/*
* If no substitutions can satisfy p->swap(*static_cast<CLASS_T*>(nullptr)
* we end up here as fallback.
*/
template<typename>
static boost::false_type test_compatible_swap_function_exists(...);
public:
typedef decltype(test_compatible_swap_function_exists<TYPE_T>(nullptr)) type;
static const bool value = type::value;
};
/**
* \brief This MetaProgramming helper class determines if there exists a method named "swap" in the ARG_T type, that takes ARG_T as an argument.
*
* If ARG_T.swap(ARG_T) is a valid function, then HasSwapMember inherits from boost::true_type. Else it inherits from boost::false_type.
*/
template<typename ARG_T>
struct HasSwapMember : public HasSwapMemberImpl<ARG_T>::type { };
这适用于我写过的几十个测试用例,所以我在这里不需要任何帮助.
相反,我正在尝试做的是检测这样的情况
struct NonReferenceSwap
{
void swap(NonReferenceSwap) {}
};
struct ReferenceSwap
{
void swap(ReferenceSwap &) {}
};
我想要一些元编程助手MetaHelperType,这样MetaHelperType继承自boost :: true_type,但是MetaHelperType会导致编译器错误地输出“交换函数必须带参考参数!”这样的消息.
或者,换句话说:
MetaHelperType<ReferenceSwap>; // IS A boost::true_type
MetaHelperType<NoSwapFunction>; // IS A boost::false_type
MetaHelperType<NonReferenceSwap>; // Compiler error
我为什么要这个?当bar作为值传递时,尝试调用foo.swap(bar)是一个无意义的概念.我的组织有很多新手c程序员,我们希望能够立即发现这个错误,而不是发现有人忘记了&两个月后.
我正在使用Visual Studio 2010,因此并非所有c 11功能都可用.上面的代码确实可以正常工作,所以至少这些功能按预期工作.
这可能吗?如果是这样,需要什么最低版本的C? C 11,C 14,C 17?
最佳答案 我使用以下内容进行精确签名检查:
#include <cstdint>
#define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \
template <typename U> \
class traitsName \
{ \
private: \
template<typename T, T> struct helper; \
template<typename T> \
static std::uint8_t check(helper<signature, &funcName>*); \
template<typename T> static std::uint16_t check(...); \
public: \
static \
constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t); \
}
DEFINE_HAS_SIGNATURE(has_ref_swap, T::swap, void (T::*)(T&));
DEFINE_HAS_SIGNATURE(has_value_swap, T::swap, void (T::*)(T));
然后,您可以编写助手检查:
template <typename T>
struct MetaHelperType_impl
{
static_assert(!has_value_swap<T>::value,
"Incorrect implementation of T::swap, "
"signature should be void T::swap(T&) instead of T::swap(T)");
using type = std::conditional_t<has_ref_swap<T>::value,
boost::true_type,
boost::false_type>;
};
template <typename T>
using MetaHelperType = typename MetaHelperType_impl<T>::type;