Templatized constructors & temp variables

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

    #1

    Templatized constructors & temp variables

    Is it possible to declare a class with a templatized constructor? I
    was sure it was, but GCC is giving me errors when I try to do this.
    Most likely user error. Some example code is:

    class MessageHeader
    {
    private:
    int m_count;

    public:
    MessageHeader(i nt count) : m_count(count) {}
    template<typena me TMessageHeader( T& iter)
    {
    m_count = set_m_count_fro m_iter(iter);
    }

    int get_count() const { return m_count; }
    };


    void foo();
    {
    vector<charbuf;
    MessageHeader header ( vec_iter(buf) );

    int count = header.get_coun t(); // Error on this line.
    }

    The exact error is:

    request for memeber 'get_count' in 'header', which is of non-class
    type 'MessageHeader ()(vec_iter)'

    Clearly MessageHeader is a class type. If I refrain from using a
    temporary variable, and instead write:

    vector<charbuf;
    vec_iter iter(buf);
    Messageheader header(iter);

    then it appears to work.

    Why is this? Thanks

  • Victor Bazarov

    #2
    Re: Templatized constructors &amp; temp variables

    Zachary Turner wrote:
    Is it possible to declare a class with a templatized constructor?
    Yes.
    I
    was sure it was, but GCC is giving me errors when I try to do this.
    Most likely user error. Some example code is:
    >
    class MessageHeader
    {
    private:
    int m_count;
    >
    public:
    MessageHeader(i nt count) : m_count(count) {}
    template<typena me TMessageHeader( T& iter)
    {
    m_count = set_m_count_fro m_iter(iter);
    'set_m_count_fr om_iter' undefined.
    }
    >
    int get_count() const { return m_count; }
    };
    >
    >
    void foo();
    {
    vector<charbuf;
    'vector' undefined.
    MessageHeader header ( vec_iter(buf) );
    The line above declares a _function_. And since I have no idea
    what 'vec_iter' is (most likely a type), I am not sure what to
    change it to, although most likely it should be

    MessageHeader header((vec_ite r)buf);
    >
    int count = header.get_coun t(); // Error on this line.
    }
    >
    The exact error is:
    >
    request for memeber 'get_count' in 'header', which is of non-class
    type 'MessageHeader ()(vec_iter)'
    >
    Clearly MessageHeader is a class type. If I refrain from using a
    temporary variable, and instead write:
    >
    vector<charbuf;
    vec_iter iter(buf);
    Messageheader header(iter);
    >
    then it appears to work.
    >
    Why is this? Thanks
    Read up on declarations.

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


    Comment

    • James Kanze

      #3
      Re: Templatized constructors &amp; temp variables

      On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      Zachary Turner wrote:
      [...]
      MessageHeader header ( vec_iter(buf) );
      >
      The line above declares a _function_. And since I have no idea
      what 'vec_iter' is (most likely a type),
      If vec_iter isn't a type, then this line doesn't declare a
      function. If vec_iter is a function or a pointer to a function,
      then this line defines a variable. And if vec_iter is anything
      else, I think the line itself is illegal. (Unless, of course,
      vec_iter is a macro. Then it's anybody's guess as to what the
      line does:-).)

      [...]
      The exact error is:
      request for memeber 'get_count' in 'header', which is of non-class
      type 'MessageHeader ()(vec_iter)'
      Clearly MessageHeader is a class type.
      If he'd have read the error message completely, he'd have seen
      that the compiler was complaining about the type of header, and
      that the compiler considered header as a "function(vec_i ter)
      returning MessageHeader". For once, the compiler error message
      is very exact, and points its finger directly to the problem.

      [...]
      Read up on declarations.
      To be fair to him, it's rather a subtle issue, and is considered
      by many to be an embarassing issue in C++. Declaration syntax
      was already seriously broken in C, and C++ just made it worse.
      (It couldn't add features otherwise, without completely breaking
      C compatibility.)

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • Victor Bazarov

        #4
        Re: Templatized constructors &amp; temp variables

        James Kanze wrote:
        On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        [...]
        >Read up on declarations.
        >
        To be fair to him, it's rather a subtle issue, and is considered
        by many to be an embarassing issue in C++. Declaration syntax
        was already seriously broken in C, and C++ just made it worse.
        (It couldn't add features otherwise, without completely breaking
        C compatibility.)
        IMNSHO it makes very little sense to expose our views on brokenness
        of C++ to a newbie, who's much better off not knowing (or even
        suspecting) those things until gaining some experience with the
        language. All goes to the bed-side manner, you know.

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


        Comment

        • Zachary  Turner

          #5
          Re: Templatized constructors &amp; temp variables

          On Jul 13, 2:55 am, James Kanze <james.ka...@gm ail.comwrote:
          On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          >
          Zachary Turner wrote:
          >
          [...]
          >
          MessageHeader header ( vec_iter(buf) );
          >
          The line above declares a _function_. And since I have no idea
          what 'vec_iter' is (most likely a type),
          >
          If vec_iter isn't a type, then this line doesn't declare a
          function. If vec_iter is a function or a pointer to a function,
          then this line defines a variable. And if vec_iter is anything
          else, I think the line itself is illegal. (Unless, of course,
          vec_iter is a macro. Then it's anybody's guess as to what the
          line does:-).)
          >
          vec_iter is declared as

          template<typena me T>
          class vec_iter
          {
          public:
          vec_iter(std::v ector<T>& rVector);
          };


          Comment

          Working...