Skip to content

[Rcpp-devel] variadic templates

8 messages · Dirk Eddelbuettel, Romain Francois

#
Hi,

they are really nice. they provide some sort of ellipsis in the template 
argument.

unfortunately, they are not part of the current standard, but in the 
forthcoming new standard c++0x. See 
http://en.wikipedia.org/wiki/C%2B%2B0x#Variadic_templates

But, gcc has many of c++0x features covered. 
http://gcc.gnu.org/projects/cxx0x.html

Why should we are about them. For example we want to mimic making this 
call:

 > call( "rnorm", 10L, 0.0, 2.0 )
rnorm(10L, 0, 2)

in C++, we (as soon as I can commit it) have the Language class, and I'd 
like calling it something like :

Language obj( "rnorm", 10, 0.0, 2.0 ) ;

The wrap functions provide conversion of each element to a SEXP, and 
variadic templates would take care of the rest.

This needs this in the Makevars :

PKG_CPPFLAGS += -I. -std=c++0x

and presumably also in RcppCxxFlags


This is available as of GCC 4.3, so I guess we could #ifdef this somehow ?

Ideas ?

Romain
#
On 2 January 2010 at 21:45, Romain Fran?ois wrote:
| Hi,
| 
| they are really nice. they provide some sort of ellipsis in the template 
| argument.
| 
| unfortunately, they are not part of the current standard, but in the 
| forthcoming new standard c++0x. See 
| http://en.wikipedia.org/wiki/C%2B%2B0x#Variadic_templates
| 
| But, gcc has many of c++0x features covered. 
| http://gcc.gnu.org/projects/cxx0x.html
| 
| Why should we are about them. For example we want to mimic making this 
| call:
| 
|  > call( "rnorm", 10L, 0.0, 2.0 )
| rnorm(10L, 0, 2)
| 
| in C++, we (as soon as I can commit it) have the Language class, and I'd 
| like calling it something like :
| 
| Language obj( "rnorm", 10, 0.0, 2.0 ) ;
| 
| The wrap functions provide conversion of each element to a SEXP, and 
| variadic templates would take care of the rest.
| 
| This needs this in the Makevars :
| 
| PKG_CPPFLAGS += -I. -std=c++0x
| 
| and presumably also in RcppCxxFlags
| 
| 
| This is available as of GCC 4.3, so I guess we could #ifdef this somehow ?
| 
| Ideas ?

I am generally in favour. The damn C++ standard and C++ implementations move
glacially. Just recently I did toy with something at work -- it may just have
standard hash_map (or now: unordered_map) and I had three possibly setups:
Boost, C++'s tr1 which is still, and also the -stc++0x you suggest here.

However -- I know that I am for example a user of Rcpp in one setting where I
am forced to use a g++ 3.4.* vintage (!!).  So we shouldn't jump too early
and require ultra-modern compilers.

So that leaves us with #ifdef / configure logic which could be more work.
But if we shield it properly and don't break the old compilers / interfaces,
we can give it a shot.

Dirk
#
On 01/02/2010 10:17 PM, Dirk Eddelbuettel wrote:
I've commited. Currently using a CXX0X define in RcppCommon.h that is 
hardcoded. I hope someone can help me generate the define from configure.

Language obj( "rnorm", 10, 0.0, 2.0 ) ;

works like a charm

Romain
2 days later
#
Hello,

I've update the conditional compiling logic so that the package may 
build on gcc < 4.3, with less functionalities since variadic templates 
were added with this release.

We still need something to generate this line of the Makevars :

PKG_CPPFLAGS += -I. -std=c++0x

-std=c++0x is only valid from gcc 4.3, so people who want to build the 
package but do not have access to gcc >= 4.3.O can just replace the line 
with this :

PKG_CPPFLAGS += -I.

I've also added Rcpp::capabilities to query the capabilities of the 
package.

For example on my system I have :

$ Rscript -e "Rcpp:::capabilities()"
variadic templates  initializer lists
               TRUE               TRUE

I'll talk about what I want to do with initializer lists later.

Romain
On 01/03/2010 09:53 AM, Romain Fran?ois wrote:

  
    
#
On 5 January 2010 at 10:15, Romain Fran?ois wrote:
| We still need something to generate this line of the Makevars :
| 
| PKG_CPPFLAGS += -I. -std=c++0x
| 
| -std=c++0x is only valid from gcc 4.3, so people who want to build the 
| package but do not have access to gcc >= 4.3.O can just replace the line 
| with this :
| 
| PKG_CPPFLAGS += -I.

I can do that quite easily in configure; RQuantLib once needed tests to
ensure that for g++ wasn't still 2.9*, and I also tests for QuantLib
versions.  Just a matter a catching a string from g++ --version and
comparing.  That will set the additional arg. for PKG_CPPFLAGS (actually: we
should use PKG_CXXFLAGS for that) and define CXX0X as 1 (and maybe we flip
the tests to  #if CXX0X  rathter than  #ifdef CXX0X)

| I've also added Rcpp::capabilities to query the capabilities of the 
| package.
| 
| For example on my system I have :
| 
| $ Rscript -e "Rcpp:::capabilities()"
| variadic templates  initializer lists
|                TRUE               TRUE

Nice one!
 
| I'll talk about what I want to do with initializer lists later.

Ok.

Dirk
#
On 01/05/2010 01:17 PM, Dirk Eddelbuettel wrote:
There is no more CXX0X in the code. There is this instead:

#ifdef __GNUC__
	#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + 
__GNUC_PATCHLEVEL__)
	#if GCC_VERSION >= 40300
		#define HAS_VARIADIC_TEMPLATES
	#endif
	#if GCC_VERSION >= 40400
		#define HAS_INIT_LISTS
	#endif
#endif


The Makevars is the only thing left to change. You seem to know how to 
do it.

  
    
#
On 5 January 2010 at 13:49, Romain Fran?ois wrote:
| There is no more CXX0X in the code. There is this instead:
| 
| #ifdef __GNUC__
| 	#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + 
| __GNUC_PATCHLEVEL__)
| 	#if GCC_VERSION >= 40300
| 		#define HAS_VARIADIC_TEMPLATES
| 	#endif
| 	#if GCC_VERSION >= 40400
| 		#define HAS_INIT_LISTS
| 	#endif
| #endif

Nice and easy. Good one. 

| The Makevars is the only thing left to change. You seem to know how to 
| do it.

We can do (and probably should as it is probably not the last time we need
something like this) this via configure, but we could even do a simple Perl
four-liner that reads the output of 'g++ --version' and blanks out -std=c++0x
accordingly.   The configure logic won't be much different but will bring
much bigger canons along for the same sparrows :)
 
| > | I've also added Rcpp::capabilities to query the capabilities of the
| > | package.
| > |
| > | For example on my system I have :
| > |
| > | $ Rscript -e "Rcpp:::capabilities()"
| > | variadic templates  initializer lists
| > |                TRUE               TRUE
| >
| > Nice one!

I saw that that gets resolved each and every time via a call to a compiled
function.  That's overkill too as this is a compile-time constant so we could
'write' the response from configure too.  Just a thought.

Idem for Rcpp:::CxxFlags() which we could fix.  Don't want to go overboard on
configure but what is known at compile time may as well get pinned down.

Dirk
#
On 01/05/2010 02:22 PM, Dirk Eddelbuettel wrote:
Who needs perl when you have R:

$ g++ --version | r -e "x <- readLines(n=1); cat(sub( '^.*[)] (.*?) 
.*$', '\\\\1', x )) "
4.4.1
Yes, but it's not called very often.