这是做一个“与”用C ++声明的最佳方式?
-
27-09-2019 - |
题
修改强>
因此,这问题是误解为这样的程度可笑它没有点了。我不知道的如何的,因为这个问题实际上我的问的是,是否的我的具体实施的这 - 是的,知道是没有意义的,是的,没有远程类似地道的C ++ - 宏为好,因为它可能是,是否一定要用到auto
,或者如果有一个合适的解决办法来代替。它不应该产生这么多的关注,肯定不是一种误解这种规模的。这是毫无意义的询问受访者编辑自己的答案,我不想让任何人失去信誉了这一点,有一些好的信息漂浮在这里为未来的潜在观众,所以我要随便挑的一个低投票答案均匀分布涉及的声誉。向前走,不要在这里看到。
我看到这个问题并决定它可能是有趣的写一个with
声明在C ++中。该auto
关键字使这很容易,但有一个更好的方式来做到这一点,或许不使用auto
?我已经消隐码的某些位为简洁。
template<class T>
struct with_helper {
with_helper(T& v) : value(v), alive(true) {}
T* operator->() { return &value; }
T& operator*() { return value; }
T& value;
bool alive;
};
template<class T> struct with_helper<const T> { ... };
template<class T> with_helper<T> make_with_helper(T& value) { ... }
template<class T> with_helper<const T> make_with_helper(const T& value) { ... }
#define with(value) \
for (auto o = make_with_helper(value); o.alive; o.alive = false)
下面是一个更典型的情况下的(更新的)的使用的例子,示出了使用with
的,因为它在其它语言中找到。
int main(int argc, char** argv) {
Object object;
with (object) {
o->member = 0;
o->method(1);
o->method(2);
o->method(3);
}
with (object.get_property("foo").perform_task(1, 2, 3).result()) {
std::cout
<< (*o)[0] << '\n'
<< (*o)[1] << '\n'
<< (*o)[2] << '\n';
}
return 0;
}
我选择o
,因为它是一种罕见的标识,它的形式给出了一个“通用的东西”的印象。如果你有一个更好的标识符或更多可用的语法完全的想法,那么请不要认为它。
解决方案
??试图VB语法成C ++
with
说,在默认情况下引用我说右做它的对象做下面的块的东西呢? 执行一系列语句使得重复参考单个对象的或结构。
with(a)
.do
.domore
.doitall
因此如何是给你相同的语法的示例
到我的为什么要使用一个例子与其中多个德referencess
所以,而不是
book.sheet.table.col(a).row(2).setColour
book.sheet.table.col(a).row(2).setFont
book.sheet.table.col(a).row(2).setText
book.sheet.table.col(a).row(2).setBorder
在有
with( book.sheet.table.col(a).row(2) )
.setColour
.setFont
.setText
.setBorder
似乎也很容易,并且用C更常见的语法++到
cell& c = book.sheet.table.col(a).row(2);
c.setColour
c.setFont
c.setText
c.setBorder
其他提示
如果您使用auto
,为什么在所有使用宏?
int main()
{
std::vector<int> vector_with_uncommonly_long_identifier;
{
auto& o = vector_with_uncommonly_long_identifier;
o.push_back(1);
o.push_back(2);
o.push_back(3);
}
const std::vector<int> constant_duplicate_of_vector_with_uncommonly_long_identifier
(vector_with_uncommonly_long_identifier);
{
const auto& o = constant_duplicate_of_vector_with_uncommonly_long_identifier;
std::cout
<< o[0] << '\n'
<< o[1] << '\n'
<< o[2] << '\n';
}
{
auto o = constant_duplicate_of_vector_with_uncommonly_long_identifier.size();
std::cout << o <<'\n';
}
}
编辑:没有auto
,只要使用typedef
和引用
int main()
{
typedef std::vector<int> Vec;
Vec vector_with_uncommonly_long_identifier;
{
Vec& o = vector_with_uncommonly_long_identifier;
o.push_back(1);
o.push_back(2);
o.push_back(3);
}
}
有关的C ++ 0x(其你假设):
int main() {
std::vector<int> vector_with_uncommonly_long_identifier;
{
auto& o = vector_with_uncommonly_long_identifier;
o.push_back(1);
o.push_back(2);
o.push_back(3);
}
}
为什么不直接使用一个良好的拉姆达?
auto func = [&](std::vector<int>& o) {
};
func(vector_with_a_truly_ridiculously_long_identifier);
一个简单的事实是,如果你的标识符是这么久了,你不能键入出来,每次使用一个参考,函数,指针等,以解决这个问题,或者更好,重构名称。像这样的语句(例如()使用C#)具有附加的副作用(确定性清理,在我的例子)。你在C ++语句没有显着的实际利益,因为它实际上并没有调用针对刚刚编写的代码进行任何额外的行为。