如何检查类型是通过 typedef 定义的还是在模板参数中使用定义的
how to check if a type is defined by typedef or using in template parameters
我想声明一个依赖于模板参数的成员类型:
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;
...
在此示例中,pointer_type
依赖于模板参数( allocator_type
(。 但是为模板参数定义pointer_type
是可选的,如果模板参数不存在此类型,我想使用默认类型,例如 value_type*
.
有什么方法可以在我的类中实现此可选成员类型定义?
这
就像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;
};
然后
using pointer_type = typename pointer_type_or_default<allocator_type>::type;
这个想法是,部分专用化是可行的,并且仅在T::pointer_type
有效并表示类型时才使用。
对于未实施 CWG 1558 决议的编译器,需要解决方法。这包括高达 4.9.2 的 GCC(当前中继版本正确编译别名模板版本,请参阅 PR 63825(,显然还有 MSVC。
相关文章:
- 部分定义/别名模板模板参数
- 有没有办法使用 googletest 创建自定义参数生成器?
- 使用自定义参数的过载删除操作员
- C++ 默认参数的重新定义:参数 1 (矢量 2D)
- Cython:具有自定义参数类型的STD ::功能回调
- 让我们C++推断定义参数的类/命名空间的位置
- 将自定义参数传递到Qt/C++中的插槽
- 定义参数包扩展的"pattern",尤其是在函数调用中
- 使用自定义参数进行 QAction
- 具有未定义参数的私有副本构造函数
- Qt-具有自定义参数的可重用绘制函数
- 类型定义参数的重载
- 如何调用定义参数为空的函数
- 如何正确使用作为其他定义参数的定义?C++
- 框架的用户定义参数应该在哪里
- 带有自定义参数的Sendmessage /postmessage
- c++定义参数重载
- Boost c++ Phoenix用户定义参数的下标操作符[]错误
- 是否可以在预处理器中定义参数化列表?
- c++中可变的用户定义参数列表