typedef & template

Discussion in 'C++' started by Dmytro, Jun 18, 2006.

  1. Dmytro

    Dmytro Guest

    Hi!

    This code is ok:

    template<class T>
    struct Container
    {
    T x;
    typedef int size_type;
    size_type size() { return sizeof T; }
    };

    Why the compiler (VC7) does not compile the following code?

    template<class T>
    struct Container
    {
    T x;
    typedef int size_type;
    size_type size() const;
    };

    template<class T>
    Container<T>::size_type Container<T>::size() { return sizeof T; }

    ---

    Thanks

    Dmytro
    Dmytro, Jun 18, 2006
    #1
    1. Advertising

  2. Dmytro

    Ian Collins Guest

    Dmytro wrote:
    > Hi!
    >
    > This code is ok:
    >
    > template<class T>
    > struct Container
    > {
    > T x;
    > typedef int size_type;
    > size_type size() { return sizeof T; }
    > };
    >
    > Why the compiler (VC7) does not compile the following code?
    >
    > template<class T>
    > struct Container
    > {
    > T x;
    > typedef int size_type;


    Why use int for a size_type? Does a negative size make sense?

    > size_type size() const;
    > };
    >
    > template<class T>
    > Container<T>::size_type Container<T>::size() { return sizeof T; }
    >

    You left out two important things:

    typename Container<T>::size_type Container<T>::size() const {
    return sizeof T; }

    The typename is required to tell the compiler that
    Container<T>::size_type is a type and the const because the prototype
    function is const.

    --
    Ian Collins.
    Ian Collins, Jun 18, 2006
    #2
    1. Advertising

  3. Dmytro <> wrote:

    > Why the compiler (VC7) does not compile the following code?
    >
    > template<class T>
    > struct Container
    > {
    > T x;
    > typedef int size_type;
    > size_type size() const;
    > };
    >
    > template<class T>
    > Container<T>::size_type Container<T>::size() { return sizeof T; }


    Where (which line in the above snippet) and which error do you get?
    My guess is you're missing a 'typename' in the last line.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Jun 18, 2006
    #3
  4. Dmytro

    Rolf Magnus Guest

    Ian Collins wrote:

    > Dmytro wrote:
    >> Hi!
    >>
    >> This code is ok:
    >>
    >> template<class T>
    >> struct Container
    >> {
    >> T x;
    >> typedef int size_type;
    >> size_type size() { return sizeof T; }
    >> };
    >>
    >> Why the compiler (VC7) does not compile the following code?
    >>
    >> template<class T>
    >> struct Container
    >> {
    >> T x;
    >> typedef int size_type;

    >
    > Why use int for a size_type? Does a negative size make sense?


    Actually, it does. If you want to be able to get an offset between two
    arbitrary elements, that offset must be a signed type and will only be able
    to span half of the range of the unsigned size type. So the size couldn't
    be more anyway. And then, it's always a bad idea to mix signed and
    unsigned, so actually, I would prefer a signed size type.
    Rolf Magnus, Jun 18, 2006
    #4
  5. Rolf Magnus posted:

    > And then, it's always a bad idea to mix
    > signed and unsigned, so actually, I would prefer a signed size type.



    Some people simply used "signed int" all the time. The situation is very
    similar to how some people will always use "i++" and have no intention of
    changing their style.

    I myself only use signed integer types when I have to. Elsewhere, I use
    unsigned integer types.



    --

    Frederick Gotham
    Frederick Gotham, Jun 18, 2006
    #5
    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. Robert A. T. Kaldy

    template typedef as return type

    Robert A. T. Kaldy, Jul 9, 2003, in forum: C++
    Replies:
    1
    Views:
    406
    Michael Kochetkov
    Jul 9, 2003
  2. Richard van Wegen

    Typedef of a template?

    Richard van Wegen, Jul 14, 2003, in forum: C++
    Replies:
    3
    Views:
    436
    Richard van Wegen
    Jul 15, 2003
  3. Adam Nielsen
    Replies:
    3
    Views:
    398
  4. oor
    Replies:
    0
    Views:
    1,329
  5. Replies:
    1
    Views:
    592
Loading...

Share This Page