Giving NULL value to non-type template parameter

N

neelsmail

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

neelsmail

Thanks for the reply.

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

neelsmail

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

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

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

    int main() {
       foo<int> fi;
       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/[email protected]/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.
 
J

James Kanze

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

James Kanze

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

James Kanze

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:

I don't see any relationship in them to the problem at hand.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top