【发布时间】:2015-04-21 12:44:58
【问题描述】:
我试图围绕这个问题 here 回想一下,因为它的编写方式隐藏了它实际在做什么。所以我把它改写成这样:
template<typename CLASS>
struct has_begin
{
// NOTE: sig_matches() must come before fn_exists() as it is used for its
// type. Also, no function bodies are needed as they are never called.
// This matching sig results in a return type of true_type
template<typename A_CLASS>
static auto
sig_matches(void(A_CLASS::*)())
-> std::true_type;
// If the member function A_CLASS::begin exists and a sig_matches() function
// exists with the required sig, then the return type is the return type of
// sig_matches(), otherwise this function can't exist because at least one
// the types don't exist so match against fn_exists(...).
template <typename A_CLASS>
static auto
fn_exists(decltype(&A_CLASS::begin))
-> decltype(sig_matches<A_CLASS>(&A_CLASS::begin));
// Member function either doesn't exist or doesn't match against a
// sig_matches() function.
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
这样做之后,发生的事情就相当容易了。然而,我发现了一些奇怪的东西。如果一个类通过 2 个开始签名传递给它,其中一个与 has_begin::sig_matches() 匹配,它将无法与它匹配。
#include <iostream>
#include <type_traits>
struct A
{
void begin()
{
std::cout << "begin() called 1" << std::endl;
}
};
struct B {};
struct C
{
void begin()
{
std::cout << "begin() called 1" << std::endl;
}
void begin(float)
{
std::cout << "begin() called 2" << std::endl;
}
};
template<typename T, typename...ARGs>
typename std::enable_if<!!has_begin<T>::value>::type
call(ARGs...args)
{
std::cout << "Found(" << has_begin<T>::value << ")" << std::endl;
T().begin(args...);
}
template<typename T, typename...ARGs>
typename std::enable_if<!has_begin<T>::value>::type
call(ARGs...)
{
std::cout << "NOT Found(" << has_begin<T>::value << ")" << std::endl;
}
int main()
{
call<A>(); // A::begin() called
call<B>(); // B has no begin()
call<C>(); // C::begin() is not called.
return 0;
}
为什么与C::begin() 匹配失败?
编辑
原因是&A_CLASS::begin 不明确。修正后的类如下:
template<typename CLASS>
struct has_begin
{
// NOTE: No function bodies are needed as they are never called.
// If the member function A_CLASS::begin exists with the required sig,
// then the return type is true_type otherwise this function can't
// exist because the type cannot be deduced.
template <typename A_CLASS>
static auto
fn_exists(decltype((void(A_CLASS::*)())&A_CLASS::begin))
-> std::true_type;
// Member function either doesn't exist or doesn't match against the
// required signature
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
Yakk 和 dyp 提出了一个很好的观点。这是一种执行相同操作但具有 compatible 签名的方法:
template<typename CLASS>
struct has_begin
{
// NOTE: No function bodies are needed as they are never called.
// If the member function A_CLASS::begin exists that has a compatible sig,
// then the return type is true_type otherwise this function can't exist
// because the type cannot be deduced.
template <typename A_CLASS>
static auto
fn_exists(decltype(std::declval<A_CLASS>().begin())*)
-> std::true_type;
// Member function either doesn't exist or doesn't match against the
// required compatible signature
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
我发现这比 Yakks 的回答更干净,因为它不需要详细的命名空间和其他“噪音”,而是 YYMV。
【问题讨论】:
-
decltype(&A_CLASS::begin)仅在&A_CLASS::begin引用单个函数时是格式正确的。在C的情况下,它指的是一个重载集,它没有单一类型。见[over.over]/1 -
通常更容易检查函数调用表达式是否格式正确,而不是检查是否可以形成指向该函数的指针。例如,
template<typename A_CLASS> static auto fn_exists(std::nullptr_t) -> decltype(std::declval<A_CLASS&>().begin(), void(), std::true_type{});这可以简化,参见例如stackoverflow.com/a/26533335 -
如果替换后存在歧义重载,则无法编译。
-
派对有点晚了@DarioOO。 ;)
-
我懒得提点 =) 我更喜欢简洁的 cmets