为什么在此模板结构中丢失了const
Why is const lost in this template structure?
已知以下功能指针具有不同的类型:
void foo_int_ref(int&);
void foo_const_int_ref(const int&);
static_assert(
!std::is_same<
decltype(foo_int_ref),
decltype(foo_const_int_ref)
>::value,
"Types should be different");
让我们考虑以下概括:
template<typename T>
void foo(T t) {}
template<typename T>
struct ref_vs_const_ref {
typedef decltype(foo<T>) foo_T;
typedef decltype(foo<const T>) foo_const_T;
};
using int_ref_vs_const_ref = ref_vs_const_ref<int&>;
static_assert(
!std::is_same<
typename int_ref_vs_const_ref::foo_T,
typename int_ref_vs_const_ref::foo_const_T
>::value,
"Types should be different"); // -- it fails
最后一个断言失败了。由于某种原因,const
丢失了foo_const_T
。但是为什么?
值参数上的const
不以任何形状或形式影响签名。当您从非网板声明中删除参考时,也是如此。const
仅影响函数定义中参数的使用。如果添加了引用或指针的类型事物变化,并且const
会影响函数的类型。
在您的嵌套类型中,T
是应用const
的int&
。但是,T&
和T& const
也是相同的类型。我想您的混乱源于您对左侧的const
的不明智的放置:它更多是const
适用于顶级实体。
已经给出了"为什么"的答案。这是解决问题的可能方法 - 将" const"添加到嵌套类型:
template<typename T, typename = void>
struct add_value_const
{
using type = const T;
};
template<typename T>
using add_value_const_t = typename add_value_const<T>::type;
template<typename T>
struct add_value_const<
T, std::enable_if_t<
std::is_reference<T>::value
>
>
{
using type = std::conditional_t<
std::is_lvalue_reference<T>::value,
add_value_const_t<std::remove_reference_t<T>>&,
add_value_const_t<std::remove_reference_t<T>>&&
>;
};
template<typename T>
struct add_value_const<
T, std::enable_if_t<std::is_pointer<T>::value>
>
{
using type = add_value_const_t<std::remove_pointer_t<T>>*;
};
在您的情况下,您必须使用foo<add_value_const<T>>
而不是foo<const T>
。
我使用了C 14语法,但可以轻松移植到C 11。使用C 17,它更可读。
您可以在Godbolt上找到汇编示例。
相关文章:
- 使用对const结构的const数组的引用进行构造
- 在只读(即 const)访问器上执行结构化绑定的最佳实践是什么?
- 如何使用函数的输出初始化 const 数组结构字段?
- 如何在类中声明静态 const 结构元素
- 获得const结构和CIN的功能
- 使用 C++ 中的自定义元素进行 Const 结构初始化
- 为什么在此模板结构中丢失了const
- 结构化绑定中的const引用扩展了分解对象的寿命
- const char指针在结构中声明
- 您如何使用成员正确初始化构造的结构,该构件是指向const值的const指针
- 用静态const成员构建类层次结构
- 如何将转换为 const char (C++) 的结构转换为结构 Objective-C
- 如何保证模板化结构中 const 静态成员的初始化顺序
- 错误:'const std::array<int, 3u>&'不是类、结构或联合类型
- C++ 成员引用基类型'Vertex *const'不是结构或联合
- 访问成员unordered_map中的数据时,C++const方法在结构中编译错误
- 为什么具有const成员的结构是可赋值的
- 使用const指针初始化const结构
- C++宏生成const结构
- 模板结构体的静态const成员的不同值