在C ++中使用“constify”操作会有意义吗? [英] Would it make sense to have a 'constify' operation in C++?

查看:156
本文介绍了在C ++中使用“constify”操作会有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++ 中有一个 constify 操作使变量 const

Would it make sense to have a "constify" operation in C/C++ that makes a variable const?

这里是一个例子,它可能是有用的,显然我们不想声明它 const ,但在第一行:

Here is an example where it could be useful, where obviously we don't want to declare it const yet in the first line:

std::vector<int> v;
v.push_back(5);
constify v; // now it's const

目前,没有这种可能性,你必须引入另一个变量得到相同的效果:

Currently, without such a possibility, you'd have to introduce another variable to get the same effect:

std::vector<int> v0;
v0.push_back(5);
const std::vector<int>& v = v0;

这更令人困惑,因为它向范围中添加了一个新名称,避免复制整个向量(或使用 swap ?)。

That's more confusing since it adds a new name into the scope and you need to make it a reference to avoid copying the whole vector (or use swap?).

推荐答案

坦率地说,如果变量是 const ,我发现更少 会混淆,比如果这可以改变。

Frankly, I find it less confusing if a variable is either const or not, than if this can change.

要详细说明一下:您通常希望这样做的原因是因为您无法初始化 const 变量的方式。 std :: vector 就是一个很好的例子。那么,一次,下一个标准引入了一个通用的初始化语法,使之成为可能:

To elaborate a bit on this: The reason you usually want to do this is because you cannot initialize a const variable the way you want to. std::vector is a good example of this. Well, for once, the next standard introduces a universal initialization syntax that makes this possible:

const std::vector<int> cvi = { 1, 2, 3, 4, 5, 42 }; 

但是,即使没有C ++ 1x的东西在手,甚至不允许这个初始化的类型语法,你总是可以创建一个帮助函数来做你想要的:

However, even without C++1x' stuff at hand, and even with types that disallow this initialization syntax, you can always create a helper function to do what you want:

const std::vector<int>& cvi = create_my_vector();

或者,如果你想成为:

const std::vector<int>& cvi = compile_time_list<1,2,3,4,5,42>::create_vector();

请注意& 。复制函数调用的结果没有意义,因为将右值绑定到 const 引用会延长其生命周期,直到引用的生命周期结束。

当然,用支持C ++ 1x'move语义的编译器重新编译将使这种优化变得几乎不必要。但是将rvlaue绑定到 const 引用可能仍然比移动向量更快,并且不太可能更慢。

使用C ++ 1x,也可能创建lambda函数做这一个飞。 C ++只是提供了一个令人难以置信的巨大的工具库。 IME,不管你有多么的想法,别人应该想出另一个想法来做同样的事情。通常是比你更好的一个。

Note the &. There's no point in copying the result of the function call, since binding an rvalue to a const reference extends its lifetime until the end of the reference's lifetime.
Of course, recompiling with a compiler that supports C++1x' move semantics will render such optimizations pretty much needless. But binding an rvlaue to a const reference might still be faster than moving a vector and is unlikely to be slower.
With C++1x, you might also create lambda functions doing this one the fly. C++ just provides an incredibly huge arsenal of tools. IME, no matter how hard you have thought, someone else ought to come up with yet another idea to do the same thing. And often a better one than yours.

但是,IME这个问题通常只有太多的代码太少的函数。然后它不仅适用于constness,也适用于类似的traits - 像一个引用指的。

一个经典的是使用一个几个可能的流。而不是这

However, IME this problem usually only comes with too much code in too few functions anyway. And then it doesn't only apply to constness, but also to similar traits - like what a reference refers to.
A classic is the use-one-of-several-possible-streams. Instead of this

int main(int argc, char* argv[])
{
  std::istream* istrm = NULL;
  std::ifstream ifs;
  if( argc > 1 )
  {
    ifs.open( argv[1] );
    if( ifs.good() ) 
      istrm = &ifs;
  }
  if( !istrm ) 
    istrm = &std::cin;

  while( istrm->good() )
  {
     // reading from *istrm implemented here
  }
  return 0;
}

只需将问题分解为1)实际读数:

just split the concerns into 1) figuring out where to read from and 2) the actual reading:

int read(std::istream& is)
{
  while( is.good() )
  {
     // reading from is implemented here
  }
  return 0;
}

int main(int argc, char* argv[])
{
  if( argc > 1 )
  {
    std::ifstream ifs( argv[1] );
    if( ifs.good() ) 
      return read(ifs);
  }
  return read(std::cin);
}



我还没有看到一个变量的现实例子, t是恒定的,因为它可能是不能通过分离关注固定的。

I have yet to see a real-world example of a variable that wasn't as constant as it could have been which couldn't be fixed by separating of concerns.

这篇关于在C ++中使用“constify”操作会有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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