[英]Overload templated class template parameters with only one class definition
基本上,我正在嘗試創建一個模板化的 class,它既可以以用戶友好的方式實例化,也可以以提供更多可配置性的更復雜的方式實例化。 我想提供這兩種實例化方法,而不必復制 API。
class 看起來類似於:
// templates that offer a lot of configurability
template<typename T, typename some_probably_awful_to_type_nested_class>
class has_my_api {
};
// templates that are easy to use
template<typename T, int i>
class has_my_api {
};
我的想法是,我可以用i
構建some_probably_awful_to_type_nested_class
的通用版本,但這兩個類將調用完全相同的 api。
我知道我可以通過使用實際 API 的內部類型並讓這兩個類中的每一個類的 API 調用內部 class 來做到這一點,但我正在尋找一種無需復制代碼即可執行此操作的方法。
這是我正在尋找的更具體的例子:
#define DEFAULT_CONFIG 0
//////////////////////////////////////////////////////////////////////
// Nested classes themselves
template<typename T>
class inner_nested_class {
// some fairly complex api
};
template<typename T,
typename inner_nested_class_t,
int configuration_flags = DEFAULT_CONFIG>
class outer_nested_class {
// some data structure of inner_nested_class
};
//////////////////////////////////////////////////////////////////////
// Helpers for creating nested_class from int seed
template<typename T, int i>
struct nested_class_creator;
template<typename T>
struct nested_class_creator<T, 0> {
typedef inner_nested_class<T> type;
};
template<typename T, int i>
struct nested_class_creator {
typedef outer_nested_class<T, typename nested_class_creator<T, i - 1>::type> type;
};
template<typename T, typename outer_nested_class_t>
class manager_class {
manager_class() = default;
int foo();
int bar();
int baz();
// some fairly involved additional API...
};
/* What i do NOT want to do
template<typename T, typename outer_nested_class_t>
class manager_class_api {
manager_class<T, outer_nested_class_t> inner_m;
int foo() { return inner_m.foo() };
int bar() { return inner_m.bar() };
int baz() { return inner_m.baz() };
};
template<typename T, int i>
class manager_class_api {
manager_class<T, typename nested_class_creator<T, i>::type> inner_m;
int foo() { return inner_m.foo() };
int bar() { return inner_m.bar() };
int baz() { return inner_m.baz() };
};
*/
/*
have tried... where the unified api could be called through _manager_class
template<typename T, int i>
using _manager_class = manager_class <T, typename nested_class_creator<T, i>::type>;
template<typename T, typename outer_nested_class_t>
using _manager_class = manager_class <T, outer_nested_class_t>;
But obviously it does not work...
*/
int
main() {
manager_class <int, outer_nested_class<int, outer_nested_class<int, inner_nested_class<int>, 0x4>, 0x3>> manager_with_user_specified_configs;
// how do I do this?
//manager_class <int, 3> manager_with_simply_api;
}
這可能嗎? 如果是這樣我該怎么做?
我很高興使用 C++ >= 11 的任何版本
注意:我知道我可以使用預處理器宏作為更壞的情況。 如果可能的話,我寧願找到一個解決方案,以便用戶可以簡單地指定一個 int 或類型。
我不認為我們可以有接受類型或相同類型值的模板。
作為解決方法,您可以將您的值包裝在一個類型 (std::integral_constant
) 中(並可能提供UDL以允許3_c
而不是std::integral_constant<int, 3>
)。
template <char... cs>
auto operator ""_c ()
{
constexpr int n = [](){
auto res = 0;
for (auto c : {cs...}) {
res *= 10;
res += c - '0';
}
return res;
}();
return std::integral_constant<int, n>{};
}
所以你會有:
template<typename T, typename outer_nested_class_t>
class manager_class {
manager_class() = default;
int foo();
int bar();
int baz();
// some fairly involved additional API...
};
template<typename T, int i>
class manager_class<T, std::integral_constant<i>> :
manager_class<T, typename nested_class_creator<T, i>::type>
{
};
您可以使用別名包裝器避免使用 inheritance:
template<typename T, typename outer_nested_class_t>
class manager_class_api {
using type = manager_class<T, outer_nested_class_t>;
};
template<typename T, int i>
class manager_class_api<T, std::integral_constant<i>>
{
using type = manager_class<T, typename nested_class_creator<T, i>::type>;
};
template<typename T, typename outer_nested_class_t>
using manager_class_api_t = typename manager_class_api<T, outer_nested_class_t>::type;
用法:
manager_class_api_t<int, outer_nested_class<int, outer_nested_class<int, inner_nested_class<int>, 0x4>, 0x3>> manager_with_user_specified_configs;
manager_class_api_t<int, decltype(3_c)> manager_with_simply_api;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.