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. Advertisements

  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. Advertisements

  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. Advertisements

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.