从模板基类继承构造函数,而不重复模板参数? [英] Inherit constructors from template base class without repeating template arguments?
问题描述
如何从模板基类继承构造函数,而不重复模板参数(而不使用宏):
例如,这不工作4.8):
template< typename T>
struct base {};
template< typename U>
struct derived:base< U> {
using base :: base;
};
如果我重复基类的模板参数,它会工作:
template< typename T>
struct base {};
template< typename U>
struct derived:base< U> {
使用base< U> :: base;
};问题是U可能是一个非常复杂的东西,很烦人,容易出现重复的错误。 。例如,以下是我最初的激励示例之一: #include< boost / multi_index_container.hpp>
#include< boost / multi_index / key_extractors.hpp>
#include& lt; boost / multi_index / ordered_index.hpp>
#include< boost / multi_index / sequenced_index.hpp>
使用命名空间boost :: multi_index;
struct as_list_tag {};
struct as_set_tag {};
template< typename T>
struct unique_list:multi_index_container<
T,
indexed_by<
sequenced< tag< as_list_tag>>,
ordered_unique< tag< as_set_tag>,identity< T&
>
> {
using multi_index_container<
T,
indexed_by<
sequenced< tag< as_list_tag>>,
ordered_unique< tag< as_set_tag>,identity< T&
>
>
:: multi_index_container;
using as_list = as_list_tag;
using as_set = as_set_tag;
};
我最后使用一个宏来解决这个问题:
#define MAKE_UNIQUE_LIST(template_params ...)\
template< typename T> \
struct unique_list:multi_index_container template_params\
{\
使用multi_index_container template_params :: multi_index_container; \
使用as_list = as_list_tag; \
使用as_set = as_set_tag; \
}
MAKE_UNIQUE_LIST(<
T,
indexed_by<
sequenced< tag< as_list_tag>>,
ordered_unique< tag< as_set_tag> ; T>>
>
>)
#undef MAKE_UNIQUE_LIST
$ b b
有更好的方法来处理吗?我缺少一些语法技巧? =)
解决方案这不是完美的,但你可以有一个类生成你的类型:
模板< typename T>
struct unique_list_base {
typedef multi_index_container<
T,
indexed_by<
sequenced< tag< as_list_tag>>,
ordered_unique< tag< as_set_tag>,identity< T&
>
>类型;
};
template< typename T>
struct unique_list:unique_list_base< T> :: type {
using unique_list_base< T> :: type :: multi_index_container;
using as_list = as_list_tag;
using as_set = as_set_tag;
};
How do I inherit constructors from a template base class without repeating the template arguments (and without using macros):
For example, this does not work (using GCC 4.8):
template <typename T>
struct base {};
template <typename U>
struct derived : base<U> {
using base::base;
};
It does work if I repeat the template arguments of the base class:
template <typename T>
struct base {};
template <typename U>
struct derived : base<U> {
using base<U>::base;
};
The problem is that "U" might be something very complex and that is annoying and error prone to repeat. For example, here is one of my original motivating examples:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
using namespace boost::multi_index;
struct as_list_tag {};
struct as_set_tag {};
template <typename T>
struct unique_list : multi_index_container <
T,
indexed_by <
sequenced<tag<as_list_tag>>,
ordered_unique<tag<as_set_tag>, identity<T>>
>
> {
using multi_index_container <
T,
indexed_by <
sequenced<tag<as_list_tag>>,
ordered_unique<tag<as_set_tag>, identity<T>>
>
>
::multi_index_container;
using as_list = as_list_tag;
using as_set = as_set_tag ;
};
I ended up working around this by using a macro:
#define MAKE_UNIQUE_LIST(template_params...)\
template <typename T>\
struct unique_list : multi_index_container template_params\
{\
using multi_index_container template_params ::multi_index_container;\
using as_list = as_list_tag;\
using as_set = as_set_tag ;\
};
MAKE_UNIQUE_LIST(<
T,
indexed_by <
sequenced<tag<as_list_tag>>,
ordered_unique<tag<as_set_tag>, identity<T>>
>
>)
#undef MAKE_UNIQUE_LIST
Is there a better way to approach this? Some syntax trick I am missing? =)
解决方案 It isn't perfect, but you could have a class that generates your type:
template <typename T>
struct unique_list_base {
typedef multi_index_container <
T,
indexed_by <
sequenced<tag<as_list_tag>>,
ordered_unique<tag<as_set_tag>, identity<T>>
>
> type;
};
template <typename T>
struct unique_list : unique_list_base<T>::type {
using unique_list_base<T>::type::multi_index_container;
using as_list = as_list_tag;
using as_set = as_set_tag ;
};
这篇关于从模板基类继承构造函数,而不重复模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!