由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个 default constructor 的问题
相关主题
why do we still use dynamic allocation?c++ question
关于C/C++里的Static variable的memory allocation/initializa一道 memset in C++的题
what is the difference?几个问题
请教一下,exception时,destructor一定会被调用么?抠字眼:assignment and initialize in C++
What does the default constructor do?请问static variable init的问题?
问个C++的operator conversation function问题c++ 一问
Is the order of initialization a, b, c or c, b, a?vector在constructor里初始化
How to initialize object in constructor?再问C++初始化问题。
相关话题的讨论汇总
话题: default话题: vector话题: int
进入Programming版参与讨论
1 (共1页)
r*********r
发帖数: 3195
1
vector x(100);
这个语句中的100个 int 到底是确定会被初始化为0, 还是可以有 junk value?
vector 对其 element 应该是 default construct, 而 int 的 default construct 应
该是 不初始化, 但是实际中好像确实所有的元素都被置零了.
N***m
发帖数: 4460
2
I tested using
#include
#include
using namespace std;
int main()
{
vector v(10);
for(int i=0;i<10;++i)
cout< cout< int j;
cout< return 0;
}
on gcc, the output is
0,0,0,0,0,0,0,0,0,0,
3650340
so it should be zeroed.
explicit vector ( size_type n, const T& value= T(), const Allocator& =
Allocator() );
on other compilers, I do not know.

【在 r*********r 的大作中提到】
: vector x(100);
: 这个语句中的100个 int 到底是确定会被初始化为0, 还是可以有 junk value?
: vector 对其 element 应该是 default construct, 而 int 的 default construct 应
: 该是 不初始化, 但是实际中好像确实所有的元素都被置零了.

r*********r
发帖数: 3195
3
这种问题 test 也没用, 都是 implementation dependent的,
要从 language spec 考虑. 不过我还没找到一个确切的说法.
t****t
发帖数: 6806
4
default initialization for int is zero-initialization. (8.5, 5)

【在 r*********r 的大作中提到】
: vector x(100);
: 这个语句中的100个 int 到底是确定会被初始化为0, 还是可以有 junk value?
: vector 对其 element 应该是 default construct, 而 int 的 default construct 应
: 该是 不初始化, 但是实际中好像确实所有的元素都被置零了.

t****t
发帖数: 6806
5
exactly, but you need to know how to read spec.

【在 r*********r 的大作中提到】
: 这种问题 test 也没用, 都是 implementation dependent的,
: 要从 language spec 考虑. 不过我还没找到一个确切的说法.

r*********r
发帖数: 3195
6
靠, 说话不用那么损吧, 搞得好像只有你会读似的.
况且, 8.5 区分了 zero-init, default-init, value-init,
这里显然不是 default-init, 而是 value-init 退化为
zero-init, 所以置零.
刚才翻了一下 bs 的书, 发现他用的是另外一个说法.
T() 当 T 是 built-in type 的时候, 只是一个 "constructor notation",
所以 int() 直接被翻译成 static_cast(0),
根本都不涉及 default construct,
只有当 T 是 user defined type 时, 才用到 default construct,
所以也避免了自相矛盾. 这也算是一个说法.

【在 t****t 的大作中提到】
: exactly, but you need to know how to read spec.
c***a
发帖数: 84
7
Default initialization of a POD object is zero initialization [§8.5, ¶
5].
r*********r
发帖数: 3195
8
FYI, this item has been amended. now it's no-op.

182;

【在 c***a 的大作中提到】
: Default initialization of a POD object is zero initialization [§8.5, ¶
: 5].

c***a
发帖数: 84
9
Sigh. My C++ knowledge seems to need some updates. (I only started to
pick it up recently after not using for almost ten years.) Can you point
me to a reference that default initialization of int is no-op?
I check a draft standard here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
And it says:
To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T
is called (and the initialization is ill-formed if T has no ac

【在 r*********r 的大作中提到】
: FYI, this item has been amended. now it's no-op.
:
: 182;

D*****n
发帖数: 218
10
0
as I remembered

【在 r*********r 的大作中提到】
: vector x(100);
: 这个语句中的100个 int 到底是确定会被初始化为0, 还是可以有 junk value?
: vector 对其 element 应该是 default construct, 而 int 的 default construct 应
: 该是 不初始化, 但是实际中好像确实所有的元素都被置零了.

相关主题
问个C++的operator conversation function问题c++ question
Is the order of initialization a, b, c or c, b, a?一道 memset in C++的题
How to initialize object in constructor?几个问题
进入Programming版参与讨论
D*****n
发帖数: 218
11
that's another story
I think

【在 r*********r 的大作中提到】
: 靠, 说话不用那么损吧, 搞得好像只有你会读似的.
: 况且, 8.5 区分了 zero-init, default-init, value-init,
: 这里显然不是 default-init, 而是 value-init 退化为
: zero-init, 所以置零.
: 刚才翻了一下 bs 的书, 发现他用的是另外一个说法.
: T() 当 T 是 built-in type 的时候, 只是一个 "constructor notation",
: 所以 int() 直接被翻译成 static_cast(0),
: 根本都不涉及 default construct,
: 只有当 T 是 user defined type 时, 才用到 default construct,
: 所以也避免了自相矛盾. 这也算是一个说法.

t****t
发帖数: 6806
12
我本来并没有损你意思, 但是看上去你确实并没有看明白8.5/5. 这里面说了三种init,
对于int来说, 都是置0, 没有一个no-op.

【在 r*********r 的大作中提到】
: 靠, 说话不用那么损吧, 搞得好像只有你会读似的.
: 况且, 8.5 区分了 zero-init, default-init, value-init,
: 这里显然不是 default-init, 而是 value-init 退化为
: zero-init, 所以置零.
: 刚才翻了一下 bs 的书, 发现他用的是另外一个说法.
: T() 当 T 是 built-in type 的时候, 只是一个 "constructor notation",
: 所以 int() 直接被翻译成 static_cast(0),
: 根本都不涉及 default construct,
: 只有当 T 是 user defined type 时, 才用到 default construct,
: 所以也避免了自相矛盾. 这也算是一个说法.

t****t
发帖数: 6806
13
can you tell me where/when it has been amended?
my standard version is
ANSI ISO/IEC 14882:2003. I believe it is the latest official version of c++
standard. If there is anything newer, please do tell me.

【在 r*********r 的大作中提到】
: FYI, this item has been amended. now it's no-op.
:
: 182;

t****t
发帖数: 6806
14
now you reminded me, i checked N3092, it does seem to be amended
6 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default
constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.
However I think this is not in effect yet and if OP wants to discuss the
incoming C++ standard

【在 c***a 的大作中提到】
: Sigh. My C++ knowledge seems to need some updates. (I only started to
: pick it up recently after not using for almost ten years.) Can you point
: me to a reference that default initialization of int is no-op?
: I check a draft standard here:
: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
: And it says:
: To default-initialize an object of type T means:
: — if T is a non-POD class type (clause 9), the default constructor for T
: is called (and the initialization is ill-formed if T has no ac

c***a
发帖数: 84
15
Wow. The language is getting more complicated every day. The terms are
confusing enough already. Their meanings even change over time!
Now back to OP's question. This is also a question for both thrust and
randomtiger: What is the value of "int()"?
My understanding is that this means a value-initialization (or default-
initialization in the old days), so its value is 0. Therefore, the
vector array must be initialized to 0's. This result actually has
nothing to do with the behavior of current def

【在 t****t 的大作中提到】
: now you reminded me, i checked N3092, it does seem to be amended
: 6 To default-initialize an object of type T means:
: — if T is a (possibly cv-qualified) class type (Clause 9), the default
: constructor for T is called (and the
: initialization is ill-formed if T has no accessible default constructor);
: — if T is an array type, each element is default-initialized;
: — otherwise, no initialization is performed.
: However I think this is not in effect yet and if OP wants to discuss the
: incoming C++ standard

t****t
发帖数: 6806
16
I believe OP's question is vector a(100) means what. this does not
involve int(), since the function signature is vector::vector(size_type n
), instead of vector::vector(size_type n, const T& value=T()).
but N3092 indeed said
explicit vector(size_type n);
3 Effects: Constructs a vector with n default constructed elements.
4 Requires: T shall be DefaultConstructible.
5 Complexity: Linear in n.
so the effect is indeed default constructed element (int) here. the question
is legitimate bu

【在 c***a 的大作中提到】
: Wow. The language is getting more complicated every day. The terms are
: confusing enough already. Their meanings even change over time!
: Now back to OP's question. This is also a question for both thrust and
: randomtiger: What is the value of "int()"?
: My understanding is that this means a value-initialization (or default-
: initialization in the old days), so its value is 0. Therefore, the
: vector array must be initialized to 0's. This result actually has
: nothing to do with the behavior of current def

c***a
发帖数: 84
17
I see. Didn't realize that the constructor signature also changed.

vector::vector(size_type n
question

【在 t****t 的大作中提到】
: I believe OP's question is vector a(100) means what. this does not
: involve int(), since the function signature is vector::vector(size_type n
: ), instead of vector::vector(size_type n, const T& value=T()).
: but N3092 indeed said
: explicit vector(size_type n);
: 3 Effects: Constructs a vector with n default constructed elements.
: 4 Requires: T shall be DefaultConstructible.
: 5 Complexity: Linear in n.
: so the effect is indeed default constructed element (int) here. the question
: is legitimate bu

r****t
发帖数: 10904
18
N3902 是啥?

n
question

【在 t****t 的大作中提到】
: I believe OP's question is vector a(100) means what. this does not
: involve int(), since the function signature is vector::vector(size_type n
: ), instead of vector::vector(size_type n, const T& value=T()).
: but N3092 indeed said
: explicit vector(size_type n);
: 3 Effects: Constructs a vector with n default constructed elements.
: 4 Requires: T shall be DefaultConstructible.
: 5 Complexity: Linear in n.
: so the effect is indeed default constructed element (int) here. the question
: is legitimate bu

r*********r
发帖数: 3195
19
N3902 是 文档编码. N3000, N3900, N3902 都是新c++标准的不同 draft.
r*********r
发帖数: 3195
20
对于 vector 在 2003标准中的定义 vector(size_t n, const T& = T())
确实是这样的, int() 表明这里要用 value-init, 即 为 0.
所以没有走 default-init 的路, 虽然 default-init 对 int 来说也是 0.
包括b-s 在内的committee member 似乎一直在讨论 initialization 的 syntax
和 semantics. 这个 value-init 的说法也是在 1998 标准之后提出来的修正.
但是在什么情况下使用 value-init 没有一个定论.
我在 google video 上找到了一个 b-s 的talk, 专门讲他想怎么在 c++0x 中
彻底解决 initialization 的问题.
从我的理解, 在 c++0x 里, vector x(3); 会得到 3 个 junk value.
如果你要得到 3 个0, 要用新的 syntax: vector x = { 0, 0, 0};
或者明确地用 vector x

【在 c***a 的大作中提到】
: I see. Didn't realize that the constructor signature also changed.
:
: vector::vector(size_type n
: question

1 (共1页)
进入Programming版参与讨论
相关主题
再问C++初始化问题。What does the default constructor do?
const object问个C++的operator conversation function问题
c++ initialize structIs the order of initialization a, b, c or c, b, a?
pthread_create inside a constructorHow to initialize object in constructor?
why do we still use dynamic allocation?c++ question
关于C/C++里的Static variable的memory allocation/initializa一道 memset in C++的题
what is the difference?几个问题
请教一下,exception时,destructor一定会被调用么?抠字眼:assignment and initialize in C++
相关话题的讨论汇总
话题: default话题: vector话题: int