C++0x 不使用构造函数进行成员初始化
在 N3257 中,我找到了一个使用的示例在没有构造函数的情况下初始化成员,这很好。我想这是可能的,因为它是一个 POD。
template<typename T>
struct adaptor {
NonStdContainer<T>* ptr; // <- data member
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{&c}) // <- init
{ /* ... */ }
}
当我玩这个例子时,我用 &
替换了 *
,因为我不喜欢原始指针:
template<typename T>
struct adaptor {
NonStdContainer<T>& ptr; // <- data member, now REF
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{c}) // <- init
{ /* ... */ }
}
这很好,并且使用 GCC-4.7 进行编译时没有警告.0。
然后我对 POD 的初始化以及 C++0x 可能发生的变化感到好奇。 在那里我找到了 Bjarnes FAQ。他在那里说 POD 可能包含指针,但没有引用。
Ops,现在我想知道:
- 这里是否有非 POD 对象,编译器无论如何都可以在没有构造函数的情况下初始化它,而我只是想念这里使用了哪些机制?
- 或者让我以这种方式初始化引用,GCC-4.7.0 的行为是否非标准?
- 或者自 Bjarnes FAQ 以来 std 是否发生了变化,也允许在 POD 中引用?
更新: 我在当前 std 中发现了聚合( 8.5.1 聚合 [dcl.init.aggr]),但是那里没有提到引用,所以我不确定它们与此有何关系
In N3257 I found an example using initializing members without a constructor, which is fine. I guess that is possible, because it is a POD.
template<typename T>
struct adaptor {
NonStdContainer<T>* ptr; // <- data member
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{&c}) // <- init
{ /* ... */ }
}
When I played around with this example I replaced the *
with a &
, because I don't like raw pointers:
template<typename T>
struct adaptor {
NonStdContainer<T>& ptr; // <- data member, now REF
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{c}) // <- init
{ /* ... */ }
}
This was fine and compiled without warning with GCC-4.7.0.
Then I got curious about the initialization of PODs and what might have changed with C++0x.
There I found Bjarnes FAQ. He says there that PODs may contain pointers, but no references.
Ops, now I wonder:
- Do I have non-POD-object here, which the compiler can initialize without a constructor anyway and I just miss which mechanisms are used here?
- or Is the GCC-4.7.0 behaving non-std by letting me initializing the ref this way?
- or has there been a change in the std since Bjarnes FAQ that also allows references in PODs?
Update: I found aggregates in the current std (8.5.1 Aggregates [dcl.init.aggr]), but references are not mentioned there, so I am not sure how they relate to this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用标准[dcl.init.aggr]:
这意味着您在这里有一个聚合,可以按照您的方式初始化聚合。 POD 与此无关,它们实际上是用于与例如进行通信。 C.
使用变量复制初始化引用当然是合法的,因为这意味着
是的,该对象是非 POD。
不。
Quoting the standard [dcl.init.aggr]:
That means you have an aggregate here, aggregates can be initialized how you do it. PODs have nothing to do with it, they are really meant for communication with eg. C.
Copy-initialization of a reference with a variable is certainly legal, because that just means
Yes, the object is non-POD.
No.