compilation issue

  • Thread starter Christof Warlich
  • Start date
C

Christof Warlich

Hi,

can anyone tell why line 6 below gives the compilation error:
error: expected primary-expression before '>' token

Everything works fine if MemoryBlockSizes is replaced by a constant,
e.g. an int.

Thanks for any hint,

Christof

template<int i>
struct Loop {
template<typename MemoryBlockSizes>
static inline unsigned int index(unsigned int size) {
if(size <= MemoryBlockSizes::value) {
Loop<i-1>::index<MemoryBlockSizes>(size);
}
else {
return i;
}
}
};
template<>
struct Loop<0> {
template<typename MemoryBlockSizes>
static inline unsigned int index(unsigned int size){
if(size <= MemoryBlockSizes::value) {
return 0xffffffff;
}
else {
return 0;
}
}
};
 
C

Christof Warlich

Christof said:
Hi,

can anyone tell why line 6 below gives the compilation error:
error: expected primary-expression before '>' token
Just found someone having Visual C++, and it compiled fine
there. So this seems to be a bug in gcc, and thus is OT here.
 
K

kwikius

Christof said:
Just found someone having Visual C++, and it compiled fine
there. So this seems to be a bug in gcc, and thus is OT here.

Havent looked closely but my guess is you need a template keyword
somewhere to distinguish < as operator less to template arg. FOR GCC


GCC is actualy technically correct whereas VC++ is not,(GCC tries to be
very pedantic IMO) but you cant blame VC8 for being smart enough to
figure it out AFAICS, even though technically wrong.

but its an annoying thing the one issue I have had a lot in porting.

regards
Andy Little
 
C

Christof Warlich

kwikius said:
Christof Warlich wrote:
Havent looked closely but my guess is you need a template keyword
somewhere to distinguish < as operator less to template arg. FOR GCC

Hi Andy,

thanks a lot for this hint, you are right. Changing line 6 from

Loop<i-1>::index<MemoryBlockSizes>(size);

to

Loop<i-1>::template index<MemoryBlockSizes>(size);

does the trick.

At least, gcc could be blamed (a bit, most of the blame is
probably on myself ;-)) for the poor error message.

Regards,

Christof
 
P

puzzlecracker

Hi Andy,

thanks a lot for this hint, you are right. Changing line 6 from

Loop<i-1>::index<MemoryBlockSizes>(size);

to

Loop<i-1>::template index<MemoryBlockSizes>(size);

does the trick.

At least, gcc could be blamed (a bit, most of the blame is
probably on myself ;-)) for the poor error message.

Regards,

Christof

Does anyone actually use meta-programming in the production code?
 
I

Ian Collins

puzzlecracker said:
Does anyone actually use meta-programming in the production code?
Yes.

I use Typelists and simple recursive templates for tasks like byte order
reversal.
 
J

James Kanze

Does anyone actually use meta-programming in the production
code?

It depends on what you mean by meta-programming (and to a lesser
degree, production code). The more advanced idioms are
obviously not to be used everywhere---in a lot of places, all
use of templates is banned at the higher application levels,
because of the coupling they introduce in the absense of export.
On the other hand, the template constructors of std::vector are
widely used, and they use (simple) meta-programming techniques
to avoid unnecessary copying if the iterators are of the correct
type, and to do the right thing if the two arguments are of
integral type. In practice, I suspect that you'll find all
sorts of variations.
 
P

Pavel

Christof said:
Hi,

can anyone tell why line 6 below gives the compilation error:
error: expected primary-expression before '>' token

Everything works fine if MemoryBlockSizes is replaced by a constant,
e.g. an int.

Thanks for any hint,

Christof

template<int i>
struct Loop {
template<typename MemoryBlockSizes>
static inline unsigned int index(unsigned int size) {
if(size <= MemoryBlockSizes::value) {
Loop<i-1>::index<MemoryBlockSizes>(size);
}
else {
return i;
}
}
};
template<>
struct Loop<0> {
template<typename MemoryBlockSizes>
static inline unsigned int index(unsigned int size){
if(size <= MemoryBlockSizes::value) {
return 0xffffffff;
}
else {
return 0;
}
}
};

Without running the code through the compiler it seems to me I see 2 issues:

1. signed/unsigned mismatch between returned value `i' and declaration
(`unsigned int index(..)')

2. Not all code paths return value in index() (you probably need
"return" -- it is right at line 6 before Loop <i-1>.

Hope this will help
-Pavel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top