Giving NULL value to non-type template parameter

Discussion in 'C++' started by neelsmail@rediffmail.com, Jul 21, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. Guest

    Thanks for the reply.

    On Jul 21, 7:14 pm, Victor Bazarov <> wrote:
    > 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, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 21, 10:41 pm, Victor Bazarov <> wrote:
    > wrote:
    > > Thanks for the reply.

    >
    > > On Jul 21, 7:14 pm, Victor Bazarov <> wrote:
    > >> 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) = 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//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, 2008
    #3
  4. James Kanze Guest

    On Jul 21, 4:01 pm, 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:
    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
     
    James Kanze, Jul 22, 2008
    #4
  5. James Kanze Guest

    On Jul 21, 4:14 pm, Victor Bazarov <> wrote:
    > 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:
    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
     
    James Kanze, Jul 22, 2008
    #5
  6. James Kanze Guest

    On Jul 21, 10:20 pm, wrote:
    > On Jul 21, 10:41 pm, Victor Bazarov <> wrote:
    > > wrote:
    > > > On Jul 21, 7:14 pm, Victor Bazarov <> wrote:
    > > >> wrote:


    > > 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);
    > > }


    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//msg15341.html


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

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jul 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ragnar
    Replies:
    2
    Views:
    495
    tom_usenet
    Sep 4, 2003
  2. Col
    Replies:
    1
    Views:
    649
    peter koch
    Apr 21, 2006
  3. sixteenmillion

    The giving that keeps on giving

    sixteenmillion, Nov 19, 2007, in forum: C Programming
    Replies:
    0
    Views:
    456
    sixteenmillion
    Nov 19, 2007
  4. Replies:
    1
    Views:
    334
    Ryan Davis
    May 6, 2005
  5. Chris McAce
    Replies:
    1
    Views:
    460
    88888 Dihedral
    Feb 8, 2012
Loading...

Share This Page