Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么“std::remove_const”与“decltype”一起使用时不删除引用对象的“const”? #定义T int int main() { 常数tx=2; //(1)`x`的类型与`T'进行比较` 静态断言(std::is_same::value,“Not same”); //(2)——将'x'的类型与'const T'进行比较` 静态断言(std::is_same::value,“Not same”); }_C++_C++11_Constants_Decltype_Reference Type - Fatal编程技术网

C++ 为什么“std::remove_const”与“decltype”一起使用时不删除引用对象的“const”? #定义T int int main() { 常数tx=2; //(1)`x`的类型与`T'进行比较` 静态断言(std::is_same::value,“Not same”); //(2)——将'x'的类型与'const T'进行比较` 静态断言(std::is_same::value,“Not same”); }

C++ 为什么“std::remove_const”与“decltype”一起使用时不删除引用对象的“const”? #定义T int int main() { 常数tx=2; //(1)`x`的类型与`T'进行比较` 静态断言(std::is_same::value,“Not same”); //(2)——将'x'的类型与'const T'进行比较` 静态断言(std::is_same::value,“Not same”); },c++,c++11,constants,decltype,reference-type,C++,C++11,Constants,Decltype,Reference Type,上述代码按预期工作。其中(1)通过且(2)失败 然而,它发生在另一个方面,即。(1) 如果我做出以下更改,则失败且(2)通过: #define T int int main () { const T x = 2; // (1) -- type of `x` is compared with `T` static_assert(std::is_same<std::remove_const<decltype(x)>::type, T>::value, "Not s

上述代码按预期工作。其中(1)通过且(2)失败

然而,它发生在另一个方面,即。(1) 如果我做出以下更改,则失败且(2)通过:

#define T int
int main ()
{
  const T x = 2;
  // (1) -- type of `x` is compared with `T`
  static_assert(std::is_same<std::remove_const<decltype(x)>::type, T>::value, "Not same");
  // (2) -- type of `x` is compared with `const T`
  static_assert(std::is_same<std::remove_const<decltype(x)>::type, const T>::value, "Not same");
}

#define T int&//因为使用了文本替换宏而不是typedef,所以得到了
const int&x

const int&
不是
const
类型,因此
remove\u const
不执行任何操作

<>不可能更改引用的代码> const >,因为C++没有任何引用的变异操作。 如果要删除最里面的
const
(其中
const T
会放置它),则此操作有效:

#define T int& // <--- added reference
模板
struct remove_deep_const_impl{typedef T type;};
模板
struct remove_deep_const_impl{typedef T type;};
模板
结构删除\u最深\u常量\u执行
{typedef typename remove_deep_const_impl::type*type;};
模板
结构删除\u最深\u常量\u执行
{typedef typename remove_deep_const_impl::type*const type;};
模板
结构删除\u最深\u常量\u执行
{typedef typename remove_deep_const_impl::type&type;};
模板
结构删除\u最深\u常量\u执行
{typedef typename remove_deep_const_impl::type&&type;};
使用remove_\u const的模板
=typename remove\u deep\u const\u impl::type;

演示:

我尝试了
删除参考
,但也没有成功。也许我没有尝试所有可能的选择。这对这种情况有帮助吗?
template <typename T>
struct remove_deepest_const_impl { typedef T type; };

template <typename T>
struct remove_deepest_const_impl<const T> { typedef T type; };

template <typename T>
struct remove_deepest_const_impl<T*>
{ typedef typename remove_deepest_const_impl<T>::type* type; };

template <typename T>
struct remove_deepest_const_impl<T* const>
{ typedef typename remove_deepest_const_impl<T>::type* const type; };

template <typename T>
struct remove_deepest_const_impl<T&>
{ typedef typename remove_deepest_const_impl<T>::type& type; };

template <typename T>
struct remove_deepest_const_impl<T&&>
{ typedef typename remove_deepest_const_impl<T>::type&& type; };

template <typename T> using remove_deepest_const
       = typename remove_deepest_const_impl<T>::type;