1
A
回答
1
爲了完整起見,我想發佈之前未提到的另一種方法。
這是基於函數的定義和別名聲明。
它遵循最小的工作示例:
#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, "!");
}
1
據this答案,你的代碼看起來是這樣的:
#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
你可能已經從this答案使用代碼,但它不具有繼承功能的工作。
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> {};
或者:
2
a solution使用void_t
,這是一個標準的C++ 17還配備了額外的工具,如is_detected_exact
在圖書館基礎V2 TS,同時大部分的工作出has_push_back
這裏的with future utilities:
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's CppCon 2015 talks。
2
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;
};
相關問題
- 1. 檢查一個對象是否從一個類型繼承
- 2. 如何檢查一個函數是否存在(但不是繼承)在Python中?
- 3. 在編譯時檢查一個函數是否具有C-linkage [不可解]
- 4. 如何檢查兩個對象是否從同一個基類繼承?
- 5. 如何編寫一個函數來檢查兩個類是否相同
- 6. 如何檢查一個函數是否是匿名函數R
- 7. 如何檢查一個Javascript函數是否是構造函數
- 8. 如何檢查一個類是否繼承另一個類而不實例化它?
- 9. 如何檢測一個類是否有移動構造函數?
- 10. 如何檢查一個對象是否有函數? (道場)
- 11. 如何檢查一個成員函數是否有const超載?
- 12. 檢查是否一個MySQL函數
- 13. 如何檢查類內的函數是否存在於另一個類中?
- 14. 如何在Scala編譯時檢查一些T是否是case類?
- 15. 如何檢查一個數是否是一個64位整數
- 16. 如何檢查一個類是否實現了一個接口而不通過其繼承樹進行導航?
- 17. 如何檢查是否泛型類型實例從另一個「繼承」,既泛型類型和參數類型
- 18. 在編譯時選擇一個函數
- 19. 檢查的對象是一個編譯時間常數
- 20. 檢查類構造函數是否擴展了另一個類
- 21. 如何在編譯時聲明一個函數是特定類的成員
- 22. 如何在iOS編譯時檢查一個特性的介紹?
- 23. 如何編寫一個檢查項目是否在樹中的函數?
- 24. 如何檢查一個ActiveRecord類是否屬於另一個
- 25. 檢查一個類是否存在
- 26. 如何判斷一個函數是否是一個類?
- 27. Java - 如何檢查類是否繼承某些類或接口?
- 28. python 3:如何檢查一個對象是否是一個函數?
- 29. 是否有可能有一個函數在另一個函數
- 30. 檢查一個特定函數是否內聯英特爾編譯器