MRB*_*MRB 2 c++ templates template-meta-programming c++11
我想声明一个依赖于模板参数的成员类型:
template< typename T >
struct bc_allocator_traits
{
public:
using this_type = bc_allocator_traits;
using allocator_type = T;
using value_type = typename allocator_type::value_type;
using pointer_type = typename allocator_type::pointer_type;
...
Run Code Online (Sandbox Code Playgroud)
在此示例中,pointer_type取决于模板参数(allocator_type).但是pointer_type为模板参数定义是可选的,如果模板参数不提供此类型,我想使用默认类型value_type*.
有没有办法在我的类中实现这个可选的成员类型定义?
T.C*_*.C. 11
这就像是后代void_t.
template<class ...>
using void_t = void;
// workaround for some compilers:
// template<class...> struct voider { using type = void; };
// template<class... Args> using void_t = typename voider<Args...>::type;
template<class T, class = void>
struct pointer_type_or_default {
using type = typename T::value_type*;
};
template<class T>
struct pointer_type_or_default<T, void_t<typename T::pointer_type>> {
using type = typename T::pointer_type;
};
Run Code Online (Sandbox Code Playgroud)
然后
using pointer_type = typename pointer_type_or_default<allocator_type>::type;
Run Code Online (Sandbox Code Playgroud)
这个想法是部分特化是可行的,只有在T::pointer_type有效并且表示类型时才使用.
对于未实施CWG 1558解决方案的编译器,需要解决方法.这包括GCC高达4.9.2(当前主干版本正确编译别名模板版本,请参阅PR 63825),显然是MSVC.
| 归档时间: |
|
| 查看次数: |
1064 次 |
| 最近记录: |