This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++ compile time calculations
- From: "Wesley Smith" <wesley dot hoke at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 21 Feb 2007 23:54:09 -0800
- Subject: g++ compile time calculations
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=WGwu99k4W4VkZ2jXrCp5WnNjeQMZ5Rv4XuQthvQfIYHx+YbeBYckuG2rxENCvxTESi4FEJ1KK7wM29AIKlqdIKY4Kn9tPpwVY09b6Idr5eG8xNTwRCoUqqSZ4OQ8lFgNdHw6gOnulsqLdZgDWPpLniJ9vWbp5jhgneMTLJef/S8=
Hi,
I'm trying to figure out how to do some compile time C++ template
programming. I have something working that calculates the length of a
string, but I'm not sure how to evaluate what is actually being done
at compile time and what is being done at run time. Here's is my
code:
template <class T>
class StringLength
{
public:
static int eval(T* a)
{ return ((*a) == '\0') ? 0 : (StringLength<T>::eval(a+1) + 1); }
};
template <class T>
inline int stringlength(T* a) { return StringLength<T>::eval(a); }
//in main somewhere
cout << stringlength<>("This is a string") << endl;
My GCC version is gcc version 4.0.1 (Apple Computer, Inc. build 5341).
I'm on OSX 10.4.8 on a PPC. Basically, I'm trying to find out what
compiler output I can look at or somehow query things at runtime to
find out what did and didn't compile down.
thanks,
wes