template constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Stippler

    #1

    template constructor

    Hello,

    short question: What is illegal about the following code?

    template <typename T>
    class Method
    {
    };

    class Procedure
    {
    public:
    template <typename T>
    Procedure(const Method<T> &rhs);

    double
    operator()(doub le x);
    };

    int
    main()
    {
    Procedure p(Method<double >());
    p(1.);

    return 0;
    }

    I want the operator() to be called with p(1.), but the compiler thinks
    different and wants to choose the constructor? Why?

    regards,
    alex
  • msalters

    #2
    Re: template constructor


    Alexander Stippler wrote:[color=blue]
    > Hello,
    >
    > short question: What is illegal about the following code?[/color]

    Nothing, it seems.
    [color=blue]
    > template <typename T>
    > class Method
    > {
    > };
    >
    > class Procedure
    > {
    > public:
    > template <typename T>
    > Procedure(const Method<T> &rhs);
    >
    > double
    > operator()(doub le x);
    > };
    >
    > int
    > main()
    > {
    > Procedure p(Method<double >());
    > p(1.);
    >
    > return 0;
    > }
    >
    > I want the operator() to be called with p(1.), but the compiler[/color]
    thinks[color=blue]
    > different and wants to choose the constructor? Why?[/color]

    No, it doesn't think that. It /does/ call the
    Procedure::Proc edure(Method<do uble>() const& rhs) constructor
    first, because that is the argument you provided when you
    defined p. Oviously, you must create p before you use p.

    Once it is constructed (the ctor returned) the compiler /will/
    call Procedure::oper ator()(double).

    Regards,
    Michiel Salters

    Comment

    • msalters

      #3
      Re: template constructor

      Alexander Stippler wrote:[color=blue]
      > Hello,
      >
      > short question: What is illegal about the following code?[/color]

      Nothing, it seems.
      [color=blue]
      > template <typename T>
      > class Method
      > {
      > };
      >
      > class Procedure
      > {
      > public:
      > template <typename T>
      > Procedure(const Method<T> &rhs);
      >
      > double
      > operator()(doub le x);
      > };
      >
      > int
      > main()
      > {
      > Procedure p(Method<double >());
      > p(1.);
      >
      > return 0;
      > }
      >
      > I want the operator() to be called with p(1.), but the compiler[/color]
      thinks[color=blue]
      > different and wants to choose the constructor? Why?[/color]

      No, it doesn't think that. It /does/ call the
      Procedure::Proc edure(Method<do uble>() const& rhs) constructor
      first, because that is the argument you provided when you
      defined p. Oviously, you must create p before you use p.

      Once it is constructed (the ctor returned) the compiler /will/
      call Procedure::oper ator()(double).

      Regards,
      Michiel Salters

      Comment

      • msalters

        #4
        Re: template constructor

        Alexander Stippler wrote:[color=blue]
        > Hello,
        >
        > short question: What is illegal about the following code?[/color]

        Nothing, it seems.
        [color=blue]
        > template <typename T>
        > class Method
        > {
        > };
        >
        > class Procedure
        > {
        > public:
        > template <typename T>
        > Procedure(const Method<T> &rhs);
        >
        > double
        > operator()(doub le x);
        > };
        >
        > int
        > main()
        > {
        > Procedure p(Method<double >());
        > p(1.);
        >
        > return 0;
        > }
        >
        > I want the operator() to be called with p(1.), but the compiler[/color]
        thinks[color=blue]
        > different and wants to choose the constructor? Why?[/color]

        No, it doesn't think that. It /does/ call the
        Procedure::Proc edure(Method<do uble>() const& rhs) constructor
        first, because that is the argument you provided when you
        defined p. Oviously, you must create p before you use p.

        Once it is constructed (the ctor returned) the compiler /will/
        call Procedure::oper ator()(double).

        Regards,
        Michiel Salters

        Comment

        • msalters

          #5
          Re: template constructor

          Alexander Stippler wrote:[color=blue]
          > Hello,
          >
          > short question: What is illegal about the following code?[/color]

          Nothing, it seems.
          [color=blue]
          > template <typename T>
          > class Method
          > {
          > };
          >
          > class Procedure
          > {
          > public:
          > template <typename T>
          > Procedure(const Method<T> &rhs);
          >
          > double
          > operator()(doub le x);
          > };
          >
          > int
          > main()
          > {
          > Procedure p(Method<double >());
          > p(1.);
          >
          > return 0;
          > }
          >
          > I want the operator() to be called with p(1.), but the compiler[/color]
          thinks[color=blue]
          > different and wants to choose the constructor? Why?[/color]

          No, it doesn't think that. It /does/ call the
          Procedure::Proc edure(Method<do uble>() const& rhs) constructor
          first, because that is the argument you provided when you
          defined p. Oviously, you must create p before you use p.

          Once it is constructed (the ctor returned) the compiler /will/
          call Procedure::oper ator()(double).

          Regards,
          Michiel Salters

          Comment

          • Alexander Stippler

            #6
            Re: template constructor

            msalters wrote:
            [color=blue]
            > Alexander Stippler wrote:[color=green]
            >> Hello,
            >>
            >> short question: What is illegal about the following code?[/color]
            >
            > Nothing, it seems.
            >[color=green]
            >> template <typename T>
            >> class Method
            >> {
            >> };
            >>
            >> class Procedure
            >> {
            >> public:
            >> template <typename T>
            >> Procedure(const Method<T> &rhs);
            >>
            >> double
            >> operator()(doub le x);
            >> };
            >>
            >> int
            >> main()
            >> {
            >> Procedure p(Method<double >());
            >> p(1.);
            >>
            >> return 0;
            >> }
            >>
            >> I want the operator() to be called with p(1.), but the compiler[/color]
            > thinks[color=green]
            >> different and wants to choose the constructor? Why?[/color]
            >
            > No, it doesn't think that. It /does/ call the
            > Procedure::Proc edure(Method<do uble>() const& rhs) constructor
            > first, because that is the argument you provided when you
            > defined p. Oviously, you must create p before you use p.
            >
            > Once it is constructed (the ctor returned) the compiler /will/
            > call Procedure::oper ator()(double).
            >
            > Regards,
            > Michiel Salters[/color]

            But icc8.1, gcc3.4 and como complain like that:

            example.cc(20): error: argument of type "double" is incompatible with
            parameter of type "Method<dou ble> (*)()"
            p(1.);



            Comment

            • Tom Widmer

              #7
              Re: template constructor

              On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
              <stip@mathemati k.uni-ulm.de> wrote:
              [color=blue]
              >Hello,
              >
              >short question: What is illegal about the following code?
              >
              >template <typename T>
              >class Method
              >{
              >};
              >
              >class Procedure
              >{
              > public:
              > template <typename T>
              > Procedure(const Method<T> &rhs);
              >
              > double
              > operator()(doub le x);
              >};
              >
              >int
              >main()
              >{
              > Procedure p(Method<double >());[/color]

              That's the declaration of a function "p" that takes a pointer to a
              function that returns a Method<double> and returns a Procedure.
              [color=blue]
              > p(1.);
              >
              > return 0;
              >}
              >
              >I want the operator() to be called with p(1.), but the compiler thinks
              >different and wants to choose the constructor? Why?[/color]

              Because it thinks p(1.) is a call of the (undefined) function you
              declared above. I think you meant:

              Procedure p((Method<doubl e>()));
              or the semantically very slightly different:
              Procedure p = Method<double>( );

              Tom

              Comment

              • Alexander Stippler

                #8
                Re: template constructor

                Tom Widmer wrote:
                [color=blue]
                > On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
                > <stip@mathemati k.uni-ulm.de> wrote:
                >[color=green]
                >>Hello,
                >>
                >>short question: What is illegal about the following code?
                >>
                >>template <typename T>
                >>class Method
                >>{
                >>};
                >>
                >>class Procedure
                >>{
                >> public:
                >> template <typename T>
                >> Procedure(const Method<T> &rhs);
                >>
                >> double
                >> operator()(doub le x);
                >>};
                >>
                >>int
                >>main()
                >>{
                >> Procedure p(Method<double >());[/color]
                >
                > That's the declaration of a function "p" that takes a pointer to a
                > function that returns a Method<double> and returns a Procedure.
                >[color=green]
                >> p(1.);
                >>
                >> return 0;
                >>}
                >>
                >>I want the operator() to be called with p(1.), but the compiler thinks
                >>different and wants to choose the constructor? Why?[/color]
                >
                > Because it thinks p(1.) is a call of the (undefined) function you
                > declared above. I think you meant:
                >
                > Procedure p((Method<doubl e>()));
                > or the semantically very slightly different:
                > Procedure p = Method<double>( );
                >
                > Tom[/color]

                Can you please explain to me the effect of the additional ()-pair? Where can
                I find this difference in the standard?

                regards,
                alex

                Comment

                • Victor Bazarov

                  #9
                  Re: template constructor

                  Alexander Stippler wrote:
                  [color=blue]
                  > Tom Widmer wrote:
                  >
                  >[color=green]
                  >>On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
                  >><stip@mathema tik.uni-ulm.de> wrote:
                  >>
                  >>[color=darkred]
                  >>>Hello,
                  >>>
                  >>>short question: What is illegal about the following code?
                  >>>
                  >>>template <typename T>
                  >>>class Method
                  >>>{
                  >>>};
                  >>>
                  >>>class Procedure
                  >>>{
                  >>> public:
                  >>> template <typename T>
                  >>> Procedure(const Method<T> &rhs);
                  >>>
                  >>> double
                  >>> operator()(doub le x);
                  >>>};
                  >>>
                  >>>int
                  >>>main()
                  >>>{
                  >>> Procedure p(Method<double >());[/color]
                  >>
                  >>That's the declaration of a function "p" that takes a pointer to a
                  >>function that returns a Method<double> and returns a Procedure.
                  >>
                  >>[color=darkred]
                  >>> p(1.);
                  >>>
                  >>> return 0;
                  >>>}
                  >>>
                  >>>I want the operator() to be called with p(1.), but the compiler thinks
                  >>>different and wants to choose the constructor? Why?[/color]
                  >>
                  >>Because it thinks p(1.) is a call of the (undefined) function you
                  >>declared above. I think you meant:
                  >>
                  >>Procedure p((Method<doubl e>()));
                  >>or the semantically very slightly different:
                  >>Procedure p = Method<double>( );
                  >>
                  >>Tom[/color]
                  >
                  >
                  > Can you please explain to me the effect of the additional ()-pair? Where can
                  > I find this difference in the standard?[/color]

                  How many times?

                  The statement:

                  <type-id> <identifier> ( <some-other-type-id> ( ) ) ;

                  is a _function_decla ration_, not an object definition. Read the FAQ.

                  V

                  Comment

                  Working...