如何在编译时检查类是否具有继承的函数
How to check if a class has an inherited function at compile-time?
#include <vector>
#include <iostream>
#include <type_traits>
using namespace std;
template<typename Coll>
class has_push_back
{
using coll_type = decay_t<Coll>;
using True = char(&)[1];
using False = char(&)[2];
template<typename U, void(U::*)(const typename U::value_type&)>
struct SFINAE {};
template<typename T>
static True Test(SFINAE<T, &T::push_back>*);
template<typename T>
static False Test(...);
public:
enum { value = sizeof(Test<coll_type>(nullptr)) == sizeof(True) };
};
class MyColl : public vector<int> {};
int main()
{
cout << has_push_back<vector<int>>::value << endl;
cout << has_push_back<MyColl>::value << endl;
}
上面的程序将输出:
1
0
它显示如果继承函数push_back
,则模板has_push_back
不起作用。
有没有办法让它工作,即使它是固有的?
template<typename Coll>
struct has_push_back {
template<
typename T,
typename = decltype(
std::declval<T&>().push_back(std::declval<typename T::value_type>())
)
>
static std::true_type Test(int);
template<typename T>
static std::false_type Test(long);
using type = decltype(Test<Coll>(0));
static constexpr bool value = type::value;
};
在线演示
这是一个使用 void_t
的解决方案,这是 C++17 的标准配置,并且还附带了其他实用程序,例如库基础 v2 TS 中的is_detected_exact
,从而消除了大部分工作has_push_back
:
template<typename... Ts>
using void_t = void;
template<typename T>
using push_back_test = decltype(std::declval<T>().push_back(std::declval<typename T::const_reference>()));
template<typename T, typename = void>
struct has_push_back : std::false_type {};
template<typename T>
struct has_push_back<T, void_t<push_back_test<T>>> : std::is_same<push_back_test<T>, void> {};
或者使用未来的实用程序:
template<typename T>
using push_back_test = decltype(std::declval<T>().push_back(std::declval<typename T::const_reference>()));
template<typename T>
using has_push_back = std::experimental::is_detected_exact<void, push_back_test, T>;
如果您想详细了解void_t
,我建议您查看Walter Brown的CppCon 2015演讲。
为了完整起见,我想发布另一种以前没有提到的方法。
这基于函数的定义和别名声明。
它遵循一个最小的工作示例:
#include <vector>
#include <type_traits>
#include<utility>
using namespace std;
template<typename T, typename... U>
constexpr auto f(int)
-> std::conditional_t<false, decltype(std::declval<T>().push_back(std::declval<U>()...)), std::true_type>;
template<typename, typename...>
constexpr std::false_type f(char);
template<typename T, typename... U>
using has_push_back = decltype(f<T, U...>(0));
class MyColl : public vector<int> {};
int main() {
static_assert(has_push_back<vector<int>, int>::value, "!");
static_assert(has_push_back<MyColl, int>::value, "!");
}
根据这个答案,你的代码可能看起来像这样:
#include <type_traits>
// Primary template with a static assertion
// for a meaningful error message
// if it ever gets instantiated.
// We could leave it undefined if we didn't care.
template<typename, typename T>
struct has_push_back {
static_assert(
std::integral_constant<T, false>::value,
"Second template parameter needs to be of function type.");
};
// specialization that does the checking
template<typename C, typename Ret, typename... Args>
struct has_push_back<C, Ret(Args...)> {
private:
template<typename T>
static constexpr auto check(T*)
-> typename
std::is_same<
decltype( std::declval<T>().push_back( std::declval<Args>()... ) ),
Ret // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>::type; // attempt to call it and see if the return type is correct
template<typename>
static constexpr std::false_type check(...);
typedef decltype(check<C>(0)) type;
public:
static constexpr bool value = type::value;
};
所有学分归@jork
您可能已经使用了此答案中的代码,但它不适用于继承的函数。
相关文章:
- 继承函数的重载解析
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 派生类调用使用非继承成员的继承函数
- 继承函数是否适用于 C++ 中的基类元素或派生类元素?
- 在链表中的某个点插入时出现问题,C++中的继承函数
- std :: is_same-来自integral_constant的继承函数的用例
- 如何将同名的继承函数视为重载函数
- 使用子函数的继承函数
- 执行覆盖的继承函数
- 相对于更专业的继承函数,模板更喜欢子类函数
- C++:继承函数并重新定义它们
- C++ 继承函数指针,可与派生方法一起使用
- 不同对象的向量,对象具有非继承函数
- C++多级继承函数调用
- 基类指向派生类继承函数调用的指针
- C++继承函数覆盖
- 派生类是否C++必须在头文件中包含继承函数/成员的定义
- 继承函数中的参数消失
- C++通过继承函数实现继承的抽象函数
- 构造函数和继承函数 c++