public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Recursive template instantiation error--help!
@ 2012-01-09  2:19 Patrick Horgan
  2012-01-09 10:06 ` Sam Varshavchik
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Horgan @ 2012-01-09  2:19 UTC (permalink / raw)
  To: GCC-help

Building with:

g++-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

This program:

template<unsigned int v>
class testit{
public:
    static const unsigned int width =
        ( v==0 ? 0 : testit<v >> 1>::width + 1);
};

int
main()
{
    unsigned int foo=testit<3>::width;
}

Errors out with complaint of exceeding maximum template instantiation
depth (error below) even though it seems it should compile like:

testit<3>
   3==0?  No, so width = testit <3 >> 1>::width + 1 ( which would be 1 + 1)
   testit<1>
       1==0? No, so width = testit<1 >> 1>::width+1 (which would be 0 + 1)
       testit<0>
       0==0 so width=0

Here's the error:

g++  -o testit testit.cpp
testit.cpp:5:40: error: template instantiation depth exceeds maximum of
1024 (use -ftemplate-depth= to increase the maximum) instantiating
‘testit<0u>::width’
testit.cpp:5:40:   recursively instantiated from ‘const unsigned int
testit<1u>::width’
testit.cpp:5:40:   instantiated from ‘const unsigned int testit<3u>::width’
testit.cpp:11:33:   instantiated from here

testit.cpp:5:40: error: template instantiation depth exceeds maximum of
1024 (use -ftemplate-depth= to increase the maximum) instantiating
‘testit<0u>::width’
testit.cpp:5:40:   recursively instantiated from ‘const unsigned int
testit<1u>::width’
testit.cpp:5:40:   instantiated from ‘const unsigned int testit<3u>::width’
testit.cpp:11:33:   instantiated from here

I also don't understand why I get the exact same sequence of errors twice.

Help!!! (and thanks;)

Patrick

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

* Re: Recursive template instantiation error--help!
  2012-01-09  2:19 Recursive template instantiation error--help! Patrick Horgan
@ 2012-01-09 10:06 ` Sam Varshavchik
  0 siblings, 0 replies; 2+ messages in thread
From: Sam Varshavchik @ 2012-01-09 10:06 UTC (permalink / raw)
  To: phorgan1; +Cc: GCC-help

[-- Attachment #1: Type: text/plain, Size: 1316 bytes --]

Patrick Horgan writes:

> Building with:
>
> g++-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
>
> This program:
>
> template<unsigned int v>
> class testit{
> public:
>     static const unsigned int width =
>         ( v==0 ? 0 : testit<v >> 1>::width + 1);
> };
>
> int
> main()
> {
>     unsigned int foo=testit<3>::width;
> }
>
> Errors out with complaint of exceeding maximum template instantiation
> depth (error below) even though it seems it should compile like:
>
> testit<3>
>    3==0?  No, so width = testit <3 >> 1>::width + 1 ( which would be 1 + 1)
>    testit<1>
>        1==0? No, so width = testit<1 >> 1>::width+1 (which would be 0 + 1)
>        testit<0>
>        0==0 so width=0

The problem is that short-circuit evaluation gets carried out only /AFTER/  
the entire expression is _fully_ parsed. So:

testit<0>:

    0 == 0 ? 0 : testit<0>::width+1;

The compiler will then proceed and attempt to expand another instance of  
testit<0>, which then obviously results in an infinite loop.

The correct way to do this is to use specialization. Instead of the ternary  
expression, specialize your template:

template<unsigned int v> class testit {
public:
  static const unsigned int width=testit<v >> 1>::width+1;
};

template<> class testit<0> {
public:
  static const unsigned int width=0;
};


[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]

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

end of thread, other threads:[~2012-01-09  2:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-09  2:19 Recursive template instantiation error--help! Patrick Horgan
2012-01-09 10:06 ` Sam Varshavchik

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