为什么使用 SFINAE 查找方法是否存在失败 std::vector::begin
Why using SFINAE to find if a method exists fails with std::vector::begin
我正在寻找一种方法来检测模板 class 是否具有方法 begin
、end
和 resize
。
我尝试了这个 answer 的修改版本:
#include <iostream>
#include <vector>
// SFINAE test
template <typename T>
class has_method
{
typedef char one;
struct two { char x[2]; };
template <typename C> static one test( decltype(&C::begin) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
int main(int argc, char *argv[])
{
std::cout << has_method<std::vector<int>>::value << std::endl;
return 0;
}
但是这会打印 0。有趣的是,这将适用于 cbegin
和 cend
,但不适用于 begin
、end
和 resize
.不过,用户定义的 classes 实现这些方法工作正常。
我已经用 g++ 和 Visual Studio 19 试过了,我得到了相同的结果,所以这似乎与编译器或 STL 的实现无关。
std::vector
有一个重载 begin
:一个重载是 const
而另一个不是。
当您编写 &C::begin
时,编译器不知道要使用哪个重载,因此它将这种歧义视为错误,由 SFINAE 检测到。
不要形成指向begin
的指针,只需调用它:
// ...
template <typename C> static one test( decltype(void(std::declval<C &>().begin())) * );
// ...
(如果不是很明显,如果您尝试检测带参数的函数,则必须提供参数,例如 .resize(0)
(或者可能 .resize(std::size_t{})
)而不仅仅是 .resize()
.)
这是另一个更短的解决方案:
#include <iostream>
#include <vector>
template <typename T, typename = void>
struct has_begin : std::false_type {};
template <typename T>
struct has_begin<T, decltype(void(std::declval<T &>().begin()))> : std::true_type {};
int main(int argc, char *argv[])
{
std::cout << has_begin<std::vector<int>>::value << std::endl;
}
这是一个基于 C++20 requires
的奇特解决方案:
#include <iostream>
#include <vector>
template <typename T>
concept has_begin = requires(T t) {t.begin();};
int main(int argc, char *argv[])
{
std::cout << has_begin<std::vector<int>> << std::endl;
}
我正在寻找一种方法来检测模板 class 是否具有方法 begin
、end
和 resize
。
我尝试了这个 answer 的修改版本:
#include <iostream>
#include <vector>
// SFINAE test
template <typename T>
class has_method
{
typedef char one;
struct two { char x[2]; };
template <typename C> static one test( decltype(&C::begin) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
int main(int argc, char *argv[])
{
std::cout << has_method<std::vector<int>>::value << std::endl;
return 0;
}
但是这会打印 0。有趣的是,这将适用于 cbegin
和 cend
,但不适用于 begin
、end
和 resize
.不过,用户定义的 classes 实现这些方法工作正常。
我已经用 g++ 和 Visual Studio 19 试过了,我得到了相同的结果,所以这似乎与编译器或 STL 的实现无关。
std::vector
有一个重载 begin
:一个重载是 const
而另一个不是。
当您编写 &C::begin
时,编译器不知道要使用哪个重载,因此它将这种歧义视为错误,由 SFINAE 检测到。
不要形成指向begin
的指针,只需调用它:
// ...
template <typename C> static one test( decltype(void(std::declval<C &>().begin())) * );
// ...
(如果不是很明显,如果您尝试检测带参数的函数,则必须提供参数,例如 .resize(0)
(或者可能 .resize(std::size_t{})
)而不仅仅是 .resize()
.)
这是另一个更短的解决方案:
#include <iostream>
#include <vector>
template <typename T, typename = void>
struct has_begin : std::false_type {};
template <typename T>
struct has_begin<T, decltype(void(std::declval<T &>().begin()))> : std::true_type {};
int main(int argc, char *argv[])
{
std::cout << has_begin<std::vector<int>>::value << std::endl;
}
这是一个基于 C++20 requires
的奇特解决方案:
#include <iostream>
#include <vector>
template <typename T>
concept has_begin = requires(T t) {t.begin();};
int main(int argc, char *argv[])
{
std::cout << has_begin<std::vector<int>> << std::endl;
}