Template parameter type constructor method call

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

    Template parameter type constructor method call

    How do I write a constructor mehtod call in this case

    /*-----------*/
    template<typena me Tclass CObjectPoolImpl
    {
    public:

    void smth(T* pObj)
    {
    if (pObj)
    pObj->T::T(); // an attempt to call a constructor method of
    class T
    }

    };

    CObjectPoolImpl <mynamespace::C MyTypeCF;
    CMyType mt;

    CF.smth(&mt);
    /*-----------*/

    MS Visual C++ 7.1:
    error C2039: 'T' : is not a member of 'mynamespace::C MyType'

    There is a reason not to write 'new T' and not to explicitly write
    constructor method name (type name). Default constructor method for
    CMyType exists.

  • Victor Bazarov

    #2
    Re: Template parameter type constructor method call

    AlexanderVX wrote:
    How do I write a constructor mehtod call in this case
    There is no "constructo r method call" in C++. What is it you're
    trying to accomplish?
    >
    /*-----------*/
    template<typena me Tclass CObjectPoolImpl
    {
    public:
    >
    void smth(T* pObj)
    {
    if (pObj)
    pObj->T::T(); // an attempt to call a constructor method of
    class T
    I am guessing here, but you might want to do

    new (pObj) T();
    }
    >
    };
    >
    CObjectPoolImpl <mynamespace::C MyTypeCF;
    CMyType mt;
    >
    CF.smth(&mt);
    This will very likely cause the constructor to be called twice for the
    same object. That has undefined behaviour, AFAICT. Why do you think
    you need it? 'mt' is already a fully constructed object.
    /*-----------*/
    >
    MS Visual C++ 7.1:
    error C2039: 'T' : is not a member of 'mynamespace::C MyType'
    >
    There is a reason not to write 'new T' and not to explicitly write
    constructor method name (type name). Default constructor method for
    CMyType exists.
    Read about "placement new".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Kai-Uwe Bux

      #3
      Re: Template parameter type constructor method call

      AlexanderVX wrote:
      How do I write a constructor mehtod call in this case
      >
      /*-----------*/
      template<typena me Tclass CObjectPoolImpl
      {
      public:
      >
      void smth(T* pObj)
      {
      if (pObj)
      pObj->T::T(); // an attempt to call a constructor method of class T
      new ( (void*) pObj ) T ();

      or:

      std::allocator< T>().construct ( pObj, T() );
      }
      >
      };
      >
      CObjectPoolImpl <mynamespace::C MyTypeCF;
      CMyType mt;
      >
      CF.smth(&mt);
      /*-----------*/
      >
      MS Visual C++ 7.1:
      error C2039: 'T' : is not a member of 'mynamespace::C MyType'
      >
      There is a reason not to write 'new T' and not to explicitly write
      constructor method name (type name). Default constructor method for
      CMyType exists.
      Note: probably, someonewill tell you that you cannot call a constructor and
      that all that you can do is cause the implementation to invoke/call it.
      That slogan refers to three facts: (a) constructor calls are not function
      calls, (b) the standard, somewhat cryptically, states that constructors
      have no names (hence they are not found through name lookup), and (c) the
      standard uses the passive voice in talking about constructors being called.

      Personally, I find it rather confusing to underscore the difference between
      functions calls and constructor calls by taking the linguistic license to
      say that we call functions but not taking the license to say that we call
      constructors. (You take a linguistic license in either case since the
      standard also uses the passive voice when talking about a function being
      called: the standard is just not concerned with what programmers do. Also,
      a function call according to the standard is just a certain form of
      expression: all you do is to write such an expression in your program and
      that causes the implementation to invoke the function in the course of
      evaluating the expression.)


      Best

      Kai-Uwe Bux

      Comment

      • AlexanderVX

        #4
        Re: Template parameter type constructor method call

        This will very likely cause the constructor to be called twice for the
        same object. That has undefined behaviour, AFAICT. Why do you think
        you need it? 'mt' is already a fully constructed object.
        You are right. This is just a sample. And the question was not about
        it...
        Read about "placement new".
        I knew, but this kind of solution was not on the top of my head...

        Comment

        • AlexanderVX

          #5
          Re: Template parameter type constructor method call

          Kai-Uwe Bux, your philosophic remark helps understanding. Good I am not
          on technical interview.

          Comment

          • AlexanderVX

            #6
            Re: Template parameter type constructor method call

            And I did it:
            pObj = new ( (void*) pObj ) T ();

            Guys, "placement new" solution results in

            error C2059: syntax error : '('

            I have an MFC project with a lot of headers included... What can cause
            this?

            Comment

            • Victor Bazarov

              #7
              Re: Template parameter type constructor method call

              AlexanderVX wrote:
              And I did it:
              pObj = new ( (void*) pObj ) T ();
              >
              Guys, "placement new" solution results in
              >
              error C2059: syntax error : '('
              >
              I have an MFC project with a lot of headers included... What can cause
              this?
              Probably not including <memory>

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • AlexanderVX

                #8
                Re: Template parameter type constructor method call

                Probably not including <memory>

                I am not quite sure why I should have included that STL header... BTW,
                this no-good project include stuff even not allowing me to use none of
                STL....

                If I include <memoryor <listor whatever STL:

                C:\Program Files\Microsoft Visual Studio .NET
                2003\Vc7\includ e\xutility(341) : error C2084: function
                'std::_Scalar_p tr_iterator_tag std::_Ptr_cat(c onst std::_Bool
                *,std::_Bool *)' already has a body
                C:\Program Files\Microsoft Visual Studio .NET
                2003\Vc7\includ e\xutility(251) : see previous definition of '_Ptr_cat'
                C:\Program Files\Microsoft Visual Studio .NET
                2003\Vc7\includ e\xutility(335) : error C2084: function
                'std::_Scalar_p tr_iterator_tag std::_Ptr_cat(s td::_Bool *,std::_Bool
                *)' already has a body
                C:\Program Files\Microsoft Visual Studio .NET
                2003\Vc7\includ e\xutility(245) : see previous definition of '_Ptr_cat'
                C:\Program Files\Microsoft Visual Studio .NET
                2003\Vc7\includ e\xutility(168) : error C2766: explicit specialization;
                'std::iterator_ traits<std::_Bo ol>' has already been defined

                And I am confused because it is not obvious what's causing it...

                But now my question is why I cannot compile such a simple thing... This
                requires include <memoryor something?

                pObj = new ( (void*) pObj ) T ();

                Comment

                • Kai-Uwe Bux

                  #9
                  Re: Template parameter type constructor method call

                  AlexanderVX wrote:
                  And I did it:
                  pObj = new ( (void*) pObj ) T ();
                  [snip]

                  Nope. Just

                  new ( (void*) pObj ) T ();

                  This is not allocating memory for you. The pointer pObj must already point
                  to the memory location where you want that object to be constructed.


                  Best

                  Kai-Uwe Bux


                  Comment

                  • AlexanderVX

                    #10
                    Re: Template parameter type constructor method call

                    Kai-Uwe Bux, thanks, it removes unwanted assignment but compilation
                    error does not go away...

                    Comment

                    • Frederick Gotham

                      #11
                      Re: Template parameter type constructor method call

                      Victor Bazarov posted:
                      Probably not including <memory>
                      You sure you don't mean <new>?

                      --

                      Frederick Gotham

                      Comment

                      • Kai-Uwe Bux

                        #12
                        Re: Template parameter type constructor method call

                        AlexanderVX wrote:
                        Kai-Uwe Bux, thanks, it removes unwanted assignment but compilation
                        error does not go away...
                        Well, the line is legal. I would guess that there is a syntax error
                        somewhere close. Please post a minimal complete example that shows the
                        problem.


                        Best

                        Kai-Uwe Bux

                        Comment

                        • Kai-Uwe Bux

                          #13
                          Re: Template parameter type constructor method call

                          Frederick Gotham wrote:
                          Victor Bazarov posted:
                          >
                          >Probably not including <memory>
                          >
                          You sure you don't mean <new>?
                          Do we need any headers for placement new? I thought, <newjust provides
                          allocation and deallocation functions. Placement new shouldn't need those,
                          or would it?


                          Best

                          Kai-Uwe Bux

                          Comment

                          • Alf P. Steinbach

                            #14
                            Re: Template parameter type constructor method call

                            * Kai-Uwe Bux:
                            Frederick Gotham wrote:
                            >
                            >Victor Bazarov posted:
                            >>
                            >>Probably not including <memory>
                            >You sure you don't mean <new>?
                            >
                            Do we need any headers for placement new? I thought, <newjust provides
                            allocation and deallocation functions. Placement new shouldn't need those,
                            or would it?
                            There are infinitely many different placement new's, but /the/ placement
                            new, the one that constructs in place, requires the header <new>. It's
                            also a good idea to use '::'. This is because it's a library feature,
                            not a language feature (although those are strongly connected here).

                            But, but, but... I hear you say, the <newheader also defines the
                            standard global allocation function invoked by ordinary 'new', and we
                            don't need to include no flickin' <newheader to use ordinary 'new'?

                            Well, that's because the ordinary 'new' is made easy to use, while the
                            placement form is made less easy to use. It's specified by ยง5.3.4/11.
                            If the placement new syntax is used (and only then), overload resolution
                            is performed to determine the allocation function to call, which
                            allocation function, in the case of /the/ placement new, is supplied by
                            the <newheader.

                            Cheers,

                            - Alf

                            --
                            A: Because it messes up the order in which people normally read text.
                            Q: Why is it such a bad thing?
                            A: Top-posting.
                            Q: What is the most annoying thing on usenet and in e-mail?

                            Comment

                            • Victor Bazarov

                              #15
                              Re: Template parameter type constructor method call

                              Kai-Uwe Bux wrote:
                              Frederick Gotham wrote:
                              >
                              >Victor Bazarov posted:
                              >>
                              >>Probably not including <memory>
                              >>
                              >You sure you don't mean <new>?
                              >
                              Do we need any headers for placement new? I thought, <newjust
                              provides allocation and deallocation functions. Placement new
                              shouldn't need those, or would it?
                              He's right, <newis what's needed, not <memory>. Placement new
                              is declared in it. See 18.4 of the Standard.

                              V
                              --
                              Please remove capital 'A's when replying by e-mail
                              I do not respond to top-posted replies, please don't ask


                              Comment

                              Working...