Connecting Tech Pros Worldwide Help | Site Map

Giving NULL value to non-type template parameter

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 21st, 2008, 02:05 PM
neelsmail@rediffmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 21st, 2008, 02:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

neelsmail@rediffmail.com wrote:
Quote:
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...
Quote:
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
  #3  
Old July 21st, 2008, 04:15 PM
neelsmail@rediffmail.com
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

Thanks for the reply.

On Jul 21, 7:14*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
neelsm...@rediffmail.com wrote:
Quote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
>
Quote:
#define non_closer ((int(*)(FILE*))0L)
>
Quote:
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?
Quote:
Quote:
but compiler throws error:
>
Quote:
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
>
Quote:
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.
Quote:
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #4  
Old July 21st, 2008, 05:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

neelsmail@rediffmail.com wrote:
Quote:
Thanks for the reply.
>
On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
>neelsm...@rediffmail.com wrote:
Quote:
>>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?
>
Quote:
Quote:
>>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
  #5  
Old July 21st, 2008, 08:25 PM
neelsmail@rediffmail.com
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

On Jul 21, 10:41*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
neelsm...@rediffmail.com wrote:
Quote:
Thanks for the reply.
>
Quote:
On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
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...
>
Quote:
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?
>
Quote:
Quote:
>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.
>
Quote:
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/gcc@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.
  #6  
Old July 22nd, 2008, 08:15 AM
James Kanze
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

On Jul 21, 4:01 pm, neelsm...@rediffmail.com wrote:
Quote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
Quote:
#define non_closer ((int(*)(FILE*))0L)
Quote:
template<class T, int F(FILE*) = non_closer>
Quote:
but compiler throws error:
Quote:
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
Quote:
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:james.kanze@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
  #7  
Old July 22nd, 2008, 08:25 AM
James Kanze
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

On Jul 21, 4:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
neelsm...@rediffmail.com wrote:
Quote:
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
Quote:
Quote:
#define non_closer ((int(*)(FILE*))0L)
Quote:
Quote:
template<class T, int F(FILE*) = non_closer>
Quote:
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.
.)
Quote:
Quote:
but compiler throws error:
Quote:
Quote:
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
Quote:
Quote:
Any idea how can I get this working?
Quote:
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:james.kanze@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
  #8  
Old July 22nd, 2008, 08:35 AM
James Kanze
Guest
 
Posts: n/a
Default Re: Giving NULL value to non-type template parameter

On Jul 21, 10:20 pm, neelsmail@rediffmail.com wrote:
Quote:
On Jul 21, 10:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
neelsm...@rediffmail.com wrote:
Quote:
On Jul 21, 7:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>neelsm...@rediffmail.com wrote:
Quote:
Quote:
I just tried the following with two compilers, they both accepted it.
Quote:
Quote:
#define zero (int(*)(int))0
Quote:
Quote:
template<class T, int (*F)(int) = zerostruct foo
{
void bar(T t) { if (F) F(666); }
};
Quote:
Quote:
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.)
Quote:
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:
I don't see any relationship in them to the problem at hand.

--
James Kanze (GABI Software) email:james.kanze@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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.