使用decltype将它转换为const [英] Using decltype to cast this to const

查看:221
本文介绍了使用decltype将它转换为const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一个问题,其中 decltype 将大大简化的事情,但我遇到一个问题使用 decltype on * this 并添加一个 const 限定符。下面的示例代码演示了此问题。

  #include< iostream& 

struct Foo
{
void bar()
{
static_cast< const decltype(* this)& ();
}

void bar()const
{
std :: cout< bar< std :: endl;
}
};

int main(int argc,char * argv [])
{
Foo f;
f.bar(); //调用非const方法
return 0;
}

代码在MSVC2010中编译,但执行递归,直到发生堆栈溢出。 / p>

Ideone 报告编译器错误

  prog.cpp:在成员函数'void Foo :: bar()':
prog.cpp:7:38:error:'const'qualifiers应用于Foo&'

如果我更改行

  static_cast< const decltype(* this)&>(* this).bar 

  static_cast< const Foo&>(* this).bar(); 

它会按预期工作。



因为表达式 * this 是错误使用或误解decltype?

解决方案

不是一个 id-expression (即它不是一个实体,像一个变量),然后 decltype(* this)表达式 * this 。该类型是 Foo& ,因此添加一个 const 限定符,并且引用它不会改变任何东西:它静默地折叠到 Foo& (下面的规则像参考崩溃),或者它是一个错误(一个const引用类型)。我不知道哪个行为是正确的,你实际上发现了两个行为不同的编译器。



您可以使用 std :: remove_reference< decltype(* this)>



如果你仍然感到困惑:

  int * p; 
// decltype(p)是变量p(或者声明的类型)的类型
// int *

// decltype((p))是类型的表达式p
// int *&因为p是一个左值

// decltype(* p)是表达式的类型* p
// int&因为* p是一个左值


I'm attempting to solve a problem in which decltype will greatly simplify things, but I'm running into an issue using decltype on *this and adding a const qualifier. The sample code below demonstrates the problem.

#include <iostream>

struct Foo
{
  void bar()
  {
    static_cast<const decltype(*this)&>(*this).bar();
  }

  void bar() const
  {
    std::cout << "bar" << std::endl;
  }
};

int main(int argc, char* argv[])
{
  Foo f;
  f.bar(); // calls non-const method
  return 0;
}

The code compiles in MSVC2010, but execution recurses until a stack overflow occurs.

Ideone reports compiler error

prog.cpp: In member function 'void Foo::bar()':
prog.cpp:7:38: error: 'const' qualifiers cannot be applied to 'Foo&'

If I change the line

static_cast<const decltype(*this)&>(*this).bar();

to

static_cast<const Foo&>(*this).bar();

it works as expected.

Am I misusing or misunderstanding decltype?

解决方案

Since the expression *this is not an id-expression (i.e. it doesn't name an entity, like a variable), then decltype(*this) gives the type of the expression *this. That type is Foo&, so adding a const qualifier and making a reference to that doesn't change anything: either it silently collapse to Foo& (following rules like reference collapsing), or it's an error (a const reference type). I'm not sure which behaviour is correct, and you have in fact found two compilers which behave differently. In any case it doesn't matter because it's not what you want.

You can use std::remove_reference<decltype(*this)>::type const& instead but that looks a bit ugly.

In case you're still confused:

int* p;
// decltype(p) is the type of the variable p (or, the declared type)
// int*

// decltype( (p) ) is the type of the expression p
// int*& because p is an lvalue

// decltype(*p) is the type of the expression *p
// int& because *p is an lvalue

这篇关于使用decltype将它转换为const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆