This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: I can not compile code from <<modern c++ design>>


At 12:00 4/8/2003 +0200, Matthias Oltmanns wrote:
Am Die, 2003-04-08 um 10.48 schrieb Di Yang:
>
> Sorry, but I still failed at this point:
>

Hmmm ... me too :-) I've tested yesterday the wrong file.

I've learned, that using sizeof with a function as parameter is a
constant integer expression and therefore valid in using
enumeration initializers.

The question remaining is, why does the following not compile:

#include <iostream>

template<typename T>
class Foo
{
public:
  static T bar();

  enum { test = sizeof(bar()) };
};

int main()
{
  std::cout << Foo<char>::test << std::endl;
}

> g++ -o conv conv.cc
conv.cc: In instantiation of `Foo<char>':
conv.cc:14:   instantiated from here
conv.cc:14: invalid use of undefined type `class Foo<char>'
conv.cc:5: declaration of `class Foo<char>'
conv.cc:14: enumerator value for `test' not integer constant

> g++ --version
g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

Is it a bug or does i still miss some point.

I don't know either, can someone familiar c++ standard explain this?


And with a FooHelper, it works:

// code start
#include <iostream>

template <typename T>
class FooHelper
{
public:
  static T bar();
};

template<typename T>
class Foo
{
public:
  enum { test = sizeof(FooHelper<T>::bar()) };
};


int main() { std::cout << Foo<char>::test << std::endl; }

// code end

Thanks.
Di

cu
Matthias


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]