473,382 Members | 1,611 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,382 software developers and data experts.

Giving NULL value to non-type template parameter

Hi,

I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:

#define non_closer ((int(*)(FILE*))0L)

template<class T, int F(FILE*) = non_closer>

but compiler throws error:

error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage

Any idea how can I get this working?

Thanks in advance,
-Neel.
Jul 21 '08 #1
7 3249
ne*******@rediffmail.com wrote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:

#define non_closer ((int(*)(FILE*))0L)

template<class T, int F(FILE*) = non_closer>
The 'F' looks like a function. Perhaps you ought to use (*F) for that?
Just a wild guess...
but compiler throws error:

error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage

Any idea how can I get this working?
Try dropping the 'L' after the 0.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '08 #2
Thanks for the reply.

On Jul 21, 7:14*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
neelsm...@rediffmail.com wrote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>

The 'F' looks like a function. *Perhaps you ought to use (*F) for that?
* Just a wild guess...
Even with *F, I get the same error: "'0u' is not a valid template
argument for type 'int (*)(FILE*)' because function '#'integer_cst'
not supported by dump_decl#<declaration error>' has not external
linkage". Is there a way to find out whether this is a SunStudio
compiler specific problem?
but compiler throws error:
error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage
Any idea how can I get this working?

Try dropping the 'L' after the 0.
Yes, that was incorrect; although even without L I get the same
errors.

Thanks for your help,
-Neel.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '08 #3
ne*******@rediffmail.com wrote:
Thanks for the reply.

On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>neelsm...@rediffmail.com wrote:
>>I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>
The 'F' looks like a function. Perhaps you ought to use (*F) for that?
Just a wild guess...

Even with *F, I get the same error: "'0u' is not a valid template
argument for type 'int (*)(FILE*)' because function '#'integer_cst'
not supported by dump_decl#<declaration error>' has not external
linkage". Is there a way to find out whether this is a SunStudio
compiler specific problem?
>>but compiler throws error:
error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage
Any idea how can I get this working?
Try dropping the 'L' after the 0.

Yes, that was incorrect; although even without L I get the same
errors.
I just tried the following with two compilers, they both accepted it.

#define zero (int(*)(int))0

template<class T, int (*F)(int) = zerostruct foo
{
void bar(T t) { if (F) F(666); }
};

int main() {
foo<intfi;
fi.bar(42);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '08 #4
On Jul 21, 10:41*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
neelsm...@rediffmail.com wrote:
Thanks for the reply.
On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
neelsm...@rediffmail.com wrote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>
The 'F' looks like a function. *Perhaps you ought to use (*F) for that?
* Just a wild guess...
Even with *F, I get the same error: "'0u' is not a valid template
argument for type 'int (*)(FILE*)' because function '#'integer_cst'
not supported by dump_decl#<declaration error>' has not external
linkage". Is there a way to find out whether this is a SunStudio
compiler specific problem?
>but compiler throws error:
error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage
Any idea how can I get this working?
Try dropping the 'L' after the 0.
Yes, that was incorrect; although even without L I get the same
errors.

I just tried the following with two compilers, they both accepted it.

* * #define zero (int(*)(int))0

* * template<class T, int (*F)(int) = zerostruct foo
* * {
* * * *void bar(T t) { *if (F) F(666); }
* * };

* * int main() {
* * * *foo<intfi;
* * * *fi.bar(42);
* * }

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
Thanks again. Even with code you have given I couldn't get compiler to
stop complaining. I found couple of articles that may explain why:

http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html
http://www.mail-archive.com/gc*@gcc..../msg15341.html

Finally, I had to rewrite the code:

template<class T, const bool Close = true, int Closer(FILE*) = fclose>
ATemplate {

static int DontClose(FILE*) {
return 0;
}

public:

ATemplate(FILE* fp):shared_ptr<FILE*>(fp, Close? Closer : DontClose)
{}
};

I will try this on MS VC 7/8, let's see if it works there..

Thanks,
-Neel.
Jul 21 '08 #5
On Jul 21, 4:01 pm, neelsm...@rediffmail.com wrote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>
but compiler throws error:
error: '0u' is not a valid template argument for type 'int (*)(FILE*)'
because function '#'integer_cst' not supported by
dump_decl#<declaration error>' has not external linkage
Any idea how can I get this working?
Wait for C++0x.

The current version of the standard specifically states that
this is illegal. The committee apparently had a change of
heart, however, and the current draft has been changed to
explicitly allow the case.

In the meantime, you'll have to define a dummy function, and
take its address, e.g.:

int non_closer( FILE* ) {}

template< typename T, int (*F)( FILE* ) = &non_closer >

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 22 '08 #6
On Jul 21, 4:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
neelsm...@rediffmail.com wrote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>
The 'F' looks like a function. Perhaps you ought to use (*F) for that?
Just a wild guess...
It would certainly be cleaner. It shouldn't make any
difference, however: "A non-type template-parameter of type
"array of T" or "function returning T" is adjusted to be of type
"pointer to T" or "pointer to function returning T",
respectively."

(Why is it that every time I see such type adjustments, I'm
reminded of a verse by Sir Walter Scott:
Oh what a tangled web we weave
When first we practice to deceive.
.)
but compiler throws error:
error: '0u' is not a valid template argument for type 'int
(*)(FILE*)' because function '#'integer_cst' not supported
by dump_decl#<declaration error>' has not external linkage
Any idea how can I get this working?
Try dropping the 'L' after the 0.
What would that change. Both 0 and 0L are legal "integral
constant expressions evaluating to 0", and so null pointer
contants.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 22 '08 #7
On Jul 21, 10:20 pm, ne*******@rediffmail.com wrote:
On Jul 21, 10:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
neelsm...@rediffmail.com wrote:
On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>neelsm...@rediffmail.com wrote:
I just tried the following with two compilers, they both accepted it.
#define zero (int(*)(int))0
template<class T, int (*F)(int) = zerostruct foo
{
void bar(T t) { if (F) F(666); }
};
int main() {
foo<intfi;
fi.bar(42);
}
It's illegal according to C++03. Maybe the compilers are
jumping the gun, and have already implemented this feature of
C++0x. (Or maybe the fact that most compilers accepted it was
the motivation for adding it to C++0x.)
Thanks again. Even with code you have given I couldn't get
compiler to stop complaining. I found couple of articles that
may explain why:
http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html
http://www.mail-archive.com/gc*@gcc..../msg15341.html
I don't see any relationship in them to the problem at hand.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 22 '08 #8

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

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
19
by: Baldur Norddahl | last post by:
Hi, How come "X=null" is not the same as "X is null"? I got a few selects with queries like this: select * from foo where customer=#customer# or (#customer# is null and customer is null) ...
9
by: ken | last post by:
Hi, I was wondering when you have an empty cell, its value is null in the debugger. So when you have an expression like this: "if len(mycell) < 1 then", or if you have "if mycell = null then" I...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
5
by: Ryan Ternier | last post by:
I'm having an issue with an SQL insert statement. It's a very simple statement, and it's causing too much fuss. strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden) VALUES("...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.