[Rcpp-devel] Initialization through constructor or through assignment?

Davor Cubranic cubranic at stat.ubc.ca
Tue Oct 25 04:39:42 CEST 2011


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ctr.cpp
Type: text/x-c++src
Size: 59 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111024/ba251a55/attachment.cpp>
-------------- next part --------------
	.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,"", at progbits
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cpy.cpp
Type: text/x-c++src
Size: 61 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111024/ba251a55/attachment-0001.cpp>
-------------- next part --------------
	.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,"", at progbits


More information about the Rcpp-devel mailing list