Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templated ctor with non-type template argument

Reply
Thread Tools

templated ctor with non-type template argument

 
 
John Harrison
Guest
Posts: n/a
 
      05-08-2004
I want to write a templated constructor with a non-type template argument,
like this.

class X
{
public:
X() : val(0) {}

template <int I>
X(X const&, X const&) : val(I) {}

private:
int val;
};

This compiles but I have no idea how to 'call' this constructor. Is it
possible?

(Essentially I have a single algorithm to construct an X from two other X's
and I would like to customise that algorithm at compile time using an
integer parameter).

john


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      05-08-2004
John Harrison wrote in news:(E-Mail Removed) in
comp.lang.c++:

> I want to write a templated constructor with a non-type template
> argument, like this.
>
> class X
> {
> public:
> X() : val(0) {}
>
> template <int I>
> X(X const&, X const&) : val(I) {}
>
> private:
> int val;
> };
>
> This compiles but I have no idea how to 'call' this constructor. Is it
> possible?
>


No.

> (Essentially I have a single algorithm to construct an X from two
> other X's and I would like to customise that algorithm at compile time
> using an integer parameter).
>


Wrap the integer up in a type, say boost::mpl::int_<> if you have
boost (*)

eg:

template < int I >
struct int_
{
};

your ctor becomes:

tempalte < int I >
X( X const&, X const&, int_< I > const & /* unused */)
: val( I )
{
}


usage:

int main()
{
X a, b, x( a , b, int_< 1 >() );
}

HTH.

*) boost::mpl -> http://www.boost.org/libs/mpl/doc/

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-08-2004

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns94E38752969E7ukcoREMOVEfreenetrtw@130.133. 1.4...
> John Harrison wrote in news:(E-Mail Removed) in
> comp.lang.c++:
>
> > I want to write a templated constructor with a non-type template
> > argument, like this.
> >
> > class X
> > {
> > public:
> > X() : val(0) {}
> >
> > template <int I>
> > X(X const&, X const&) : val(I) {}
> >
> > private:
> > int val;
> > };
> >
> > This compiles but I have no idea how to 'call' this constructor. Is it
> > possible?
> >

>
> No.
>
> > (Essentially I have a single algorithm to construct an X from two
> > other X's and I would like to customise that algorithm at compile time
> > using an integer parameter).
> >

>
> Wrap the integer up in a type, say boost::mpl::int_<> if you have
> boost (*)
>
> eg:
>
> template < int I >
> struct int_
> {
> };
>
> your ctor becomes:
>
> tempalte < int I >
> X( X const&, X const&, int_< I > const & /* unused */)
> : val( I )
> {
> }
>
>
> usage:
>
> int main()
> {
> X a, b, x( a , b, int_< 1 >() );
> }
>
> HTH.
>


OK, that's clever. Thanks.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference to pointer used in ctor (using 'this' in ctor) Anonymous C++ 2 08-28-2007 01:10 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments