473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Arguments for templates with value parameters

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

Jul 22 '05 #1
7 3138

"DaKoadMunky" <da*********@aol.com> wrote in message
<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

Jul 22 '05 #2

"Sharad Kala" <no******************@yahoo.com> schrieb im Newsbeitrag
news:2r*************@uni-berlin.de...

"DaKoadMunky" <da*********@aol.com> wrote in message
<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

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)
Jul 22 '05 #3
DaKoadMunky wrote in news:20***************************@mb-m14.aol.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
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
Jul 22 '05 #5

"Gernot Frisch" <Me@Privacy.net> wrote in message
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
Jul 22 '05 #6

"Greg Comeau" <co****@panix.com> wrote in message
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
Jul 22 '05 #7
In article <2r*************@uni-berlin.de>,
Sharad Kala <no******************@yahoo.com> wrote:
"Greg Comeau" <co****@panix.com> wrote in message
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 ?

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.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Birgit Rahm | last post by:
Hallo Newsgroup, I have the following problem: I work with Python 2.2 and invoke functions via CORBA ( I use onmiORB/omniORBpy) on a server. The server provides me a function, where the 3...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
5
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
2
by: Martin Gernhard | last post by:
Hi, I'm trying to use expression templates to calculate values at compile time. Doing it with just one parameter is no problem: ///Begin Listing 1 #include <iostream> using namespace std; ...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
1
by: Andy | last post by:
Hi, why does with-param fail to send parameters to any templates in a stylesheet that also accepts arguments from the ASP.NET XML control? The argument from the XML control seems to be available...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.