Arguments for templates with value parameters

D

DaKoadMunky

<CODE>

template<int N>
int ReturnN()
{
return N;
}

int anInt = 32;

int main()
{
ReturnN<16>();

ReturnN<anInt>();

return 0;
}

</CODE>

The code above compiles using VC++.NET 7.00.9466.

It fails to compile using the online compiler at
www.comeaucomputing.com/tryitout (default settings with target of Windows XP)
with the following message...

<ERROR>

"ComeauTest.c", line 13: error: expression must have a constant value
ReturnN<anInt>();
^

1 error detected in the compilation of "ComeauTest.c".

</ERROR>

Can anyone tell me which compiler is correct?

I assume it is Comeau, but you never know.

FYI...The VC++.NET program does produce correct results. The version of the
function invoked by "ReturnN<anInt>()" instead of returning a hardcoded value
actually returns the result of dereferencing a hardcoded address (in this case
the address of the global variable anInt.)

Thanks.
 
S

Sharad Kala

DaKoadMunky said:
<CODE>

template<int N>
int ReturnN()
{
return N;
}

int anInt = 32;

int main()
{
ReturnN<16>();

ReturnN<anInt>();

return 0;
}

</CODE>

The code above compiles using VC++.NET 7.00.9466.

It fails to compile using the online compiler at
www.comeaucomputing.com/tryitout (default settings with target of Windows XP)
with the following message...

Can anyone tell me which compiler is correct?

Comeau, reason being that template arguments must be known at compile time.
Comeau will accept the code if you make anInt const or a macro.

Sharad
 
R

Rob Williscroft

DaKoadMunky wrote in in
comp.lang.c++:
Can anyone tell me which compiler is correct?

I assume it is Comeau, but you never know.

Its Comeau, non-type template arguments must be compile-time
integral constants, or constant pointers or references.

template < int N > int f() { return N; }

enum Named_enum { value = 1 };
int const another = 2;

struct aStruct
{
static int const value = 3;
};

struct anotherStruct
{
enum Named_enum { value = 4 };
};

char array[5];

int main()
{
f< value >();
f< another >();
f< aStruct::value >();
f< anotherStruct::value >();

f< sizeof( array ) >();
}

Note that sizeof( array ) is a std::size_t integral constant,
but the rules allow it to be converted to an int integral const.

I gave the enum's a name (i.e not enum { value = 1 };) as with
some older compilers this helps, but strictly speaking it isn't
required.

HTH.

Rob.
 
G

Gernot Frisch

Sharad Kala said:
Comeau, reason being that template arguments must be known at
compile time.
Comeau will accept the code if you make anInt const or a macro.

Sharad


So, does VC then use 'typeof anInt' or simply use an 'int' if it
doesn't know what to use? (The way VC 6 did things, I think)
 
J

JKop

According to the Standard, the program is illformed.

But in practicality, the aformentioned compiler *can*
figure out what's going on and it proceeds in such a
fashion.

I'd say there all sorts of compiler switches for the
aforementioned compiler that will actually compiler your
code successfully but then say:

Note: Standard C++ forbids non-const template parameter.

-JKop
 
S

Sharad Kala

Gernot Frisch said:
So, does VC then use 'typeof anInt' or simply use an 'int' if it
doesn't know what to use? (The way VC 6 did things, I think)

I have no idea as to how VC is compiling it. Also note that VC 6 has quite
poor template support.

Sharad
 
G

Greg Comeau

<CODE>

template<int N>
int ReturnN()
{
return N;
}

int anInt = 32;

int main()
{
ReturnN<16>();

ReturnN<anInt>();

return 0;
}

</CODE>

The code above compiles using VC++.NET 7.00.9466.

It fails to compile using the online compiler at
www.comeaucomputing.com/tryitout (default settings with target of Windows XP)
with the following message...

<ERROR>

"ComeauTest.c", line 13: error: expression must have a constant value
ReturnN<anInt>();
^

1 error detected in the compilation of "ComeauTest.c".

</ERROR>

Can anyone tell me which compiler is correct?

I assume it is Comeau, but you never know.

FYI...The VC++.NET program does produce correct results. The version of the
function invoked by "ReturnN<anInt>()" instead of returning a hardcoded value
actually returns the result of dereferencing a hardcoded address (in this case
the address of the global variable anInt.)

In addition to Comeau C++, VC++ 6.0 and VC++ 7.1 also give errors
for line 13, as they should, so this must be a VC++ 7.0ism.

BTW, Comeau online does not have a target of Windows XP,
it's target is a faux platform.
 
G

Greg Comeau

Comeau, reason being that template arguments must be known at compile time.
Comeau will accept the code if you make anInt const or a macro.

It's not just that the arg must be known, or be const,
it's that when using something like 'int N' then it's
preferring an int constant as the argument.
 
S

Sharad Kala

Greg Comeau said:
It's not just that the arg must be known, or be const,
it's that when using something like 'int N' then it's
preferring an int constant as the argument.

Could you elaborate as to what you mean by 'preferring' in this context ?

Thanks,
Sharad
 
G

Greg Comeau

Could you elaborate as to what you mean by 'preferring' in this context ?


C:\Comeau C++>type intn.cpp
template <int N>
struct xyz {};

int i;
const int ci = 99;

enum { blah };

int main()
{
// these are "preferred", as in allowed:
xyz<999> x999;
xyz<ci> x99;
xyz<'c'> xc;
xyz<blah> xblah;

// these are errors:
xyz<99.99> x99d99;
xyz<i> xi;

return 0;
}

C:\Comeau C++>como --A intn.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C++

"intn.cpp", line 18: error: expression must have integral or enum type
xyz<99.99> x99d99;
^

"intn.cpp", line 19: error: expression must have a constant value
xyz<i> xi;
^

.....

So i is known, and 99.99 is a constant, but that alone is insufficient.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top