On October 21, 2011 01:12:11 PM Douglas Bates wrote:
> Perhaps this is an indication that I should read some sections of "C++
> Annotations" again. I am trying to remember the pros and cons of
> initializing a class instance in C++ through assignment or through the
> copy constructor.
I remember reading somewhere that the standard guarantees that this is exactly
the same. (I didn't read the standard myself, and so either my source or my
memory could be faulty.)
> As a much more trivial example, in C I would write
>
> int one = 1;
>
> but in C++ I tend to write
>
> int one(1);
Here is a test, compiling the attached ctr.cpp and cpy.cpp with "gcc -S". (GCC
version 4.5.2) You can see that the assembly output is identical (ctr.s and
cpy.s, respectively). This is for ints, so more complicated objects, like
NumericMatrix in Dirk's benchmarks, could be compiled differently, I suppose.
Davor
int foo(int a) {
int b(a);
int c(3);
return b - c;
}
.file "ctr.cpp"
.text
.globl _Z3fooi
.type _Z3fooi, @function
_Z3fooi:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
subl $16, %esp
movl 8(%ebp), %eax
movl %eax, -4(%ebp)
movl $3, -8(%ebp)
movl -8(%ebp), %eax
movl -4(%ebp), %edx
movl %edx, %ecx
subl %eax, %ecx
movl %ecx, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size _Z3fooi, .-_Z3fooi
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
int foo(int a) {
int b = a;
int c = 3;
return b - c;
}
.file "cpy.cpp"
.text
.globl _Z3fooi
.type _Z3fooi, @function
_Z3fooi:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
subl $16, %esp
movl 8(%ebp), %eax
movl %eax, -4(%ebp)
movl $3, -8(%ebp)
movl -8(%ebp), %eax
movl -4(%ebp), %edx
movl %edx, %ecx
subl %eax, %ecx
movl %ecx, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size _Z3fooi, .-_Z3fooi
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel