By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

templated ctor with non-type template argument

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John Harrison wrote in news:2g************@uni-berlin.de 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/
Jul 22 '05 #2

P: n/a

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
John Harrison wrote in news:2g************@uni-berlin.de 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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.