templated ctor with non-type template argument

Discussion in 'C++' started by John Harrison, May 8, 2004.

  1. 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
     
    John Harrison, May 8, 2004
    #1
    1. Advertising

  2. John Harrison wrote in news: 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/
     
    Rob Williscroft, May 8, 2004
    #2
    1. Advertising

  3. "Rob Williscroft" <> wrote in message
    news:Xns94E38752969E7ukcoREMOVEfreenetrtw@130.133.1.4...
    > John Harrison wrote in news: 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
     
    John Harrison, May 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    530
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    489
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    489
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    412
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. nguillot
    Replies:
    5
    Views:
    532
Loading...

Share This Page