public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ compile time calculations
@ 2007-02-22 10:16 Wesley Smith
  2007-02-22 13:36 ` Daniel Lohmann
  0 siblings, 1 reply; 5+ messages in thread
From: Wesley Smith @ 2007-02-22 10:16 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++ compile time calculations
  2007-02-22 10:16 g++ compile time calculations Wesley Smith
@ 2007-02-22 13:36 ` Daniel Lohmann
  2007-02-22 17:12   ` Wesley Smith
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Lohmann @ 2007-02-22 13:36 UTC (permalink / raw)
  To: Wesley Smith; +Cc: gcc-help



Wesley Smith schrieb:
> 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.


I suggest taking a close look at the generate assembler code, e.g. using 
objdump:

objdump -D --demangle --reloc objfile.o | less

However, without taking a close look at the generated code,  I am quite 
sure that there isn't much that is compiled down. Your template isn't 
actually a "template meta program" - it does not use recursive 
instantiation of templates, but just recursive function calls:

          static int eval(T* a)
                  {    return ((*a) == '\0') ? 0 :
(StringLength<T>::eval(a+1) + 1);  }

This is always the same StringLength::eval() method that is recursively 
invoked at *runtime*.

Daniel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++ compile time calculations
  2007-02-22 13:36 ` Daniel Lohmann
@ 2007-02-22 17:12   ` Wesley Smith
  2007-02-22 18:11     ` Daniel Llorens del Río
  2007-02-23 12:10     ` Daniel Lohmann
  0 siblings, 2 replies; 5+ messages in thread
From: Wesley Smith @ 2007-02-22 17:12 UTC (permalink / raw)
  To: gcc-help

Is it actually possible to process the characters of a string during
the C++ compilation process or is this something that is impossible
due to the C++ spec?  I'm startign to think it isn't possible at all
and that only number types like the famous Factorial template can be
handled this way.  What document could I look in to find this out?

thanks,
wes

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++ compile time calculations
  2007-02-22 17:12   ` Wesley Smith
@ 2007-02-22 18:11     ` Daniel Llorens del Río
  2007-02-23 12:10     ` Daniel Lohmann
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Llorens del Río @ 2007-02-22 18:11 UTC (permalink / raw)
  To: gcc-help


On 22 Feb, 2007, at 18:01, Wesley Smith wrote:

> Is it actually possible to process the characters of a string during
> the C++ compilation process or is this something that is impossible
> due to the C++ spec?  I'm startign to think it isn't possible at all
> and that only number types like the famous Factorial template can be
> handled this way.  What document could I look in to find this out?
>
> thanks,
> wes

For your problem, it would be necessary to convert the string into a  
type that represents the string, to pass that as a template  
parameter. But you can't use [] nor * on a char * at compile time.

On the other hand, sizeof will do what you want without any templates.

Anyway, you are likely to receive better answers from comp.lang.c++  
or comp.lang.c++.moderated.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++ compile time calculations
  2007-02-22 17:12   ` Wesley Smith
  2007-02-22 18:11     ` Daniel Llorens del Río
@ 2007-02-23 12:10     ` Daniel Lohmann
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Lohmann @ 2007-02-23 12:10 UTC (permalink / raw)
  To: Wesley Smith; +Cc: gcc-help

Wesley Smith wrote:
> Is it actually possible to process the characters of a string during
> the C++ compilation process or is this something that is impossible
> due to the C++ spec?  I'm startign to think it isn't possible at all
> and that only number types like the famous Factorial template can be
> handled this way.  What document could I look in to find this out?

In don't think that this is possible. While you can, in general, use
pointers as (value-type) template parameters, the standard explicitly
forbids using a string literal to instantiate templates (AFAIR).

However, such question does not really belong to gcc-help. You might
want to seek for help on a general C++ mailing list.


Daniel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-02-23  8:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-22 10:16 g++ compile time calculations Wesley Smith
2007-02-22 13:36 ` Daniel Lohmann
2007-02-22 17:12   ` Wesley Smith
2007-02-22 18:11     ` Daniel Llorens del Río
2007-02-23 12:10     ` Daniel Lohmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).