Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:15:41+00:00 2026-06-11T23:15:41+00:00

I understand from bunch of other Stackoverflow threads (like this ) that Template arguments

  • 0

I understand from bunch of other Stackoverflow threads (like this) that Template arguments are evaluated at Compile Time.
Also, non-type template-parameter should be either a constant expression, integral expression or pointer to an object with external linkage.

And, I am not using –std=c++0x in my g++ command in Makefile.

So, is it possible to instantiate a template class with NULL passed as argument?

// I have a template class like this - 
template<class T, T invalidVal, int e> class A
{ 
    static inline bool dummy(T value) 
    {
       return 0;
    }
}

#define MY_INVALID_VAL ((void *)0)

// Now I want create class from the above template class with 
// T=void*, invalidVal=NULL & e=0 
typedef A<void *, MY_INVALID_VAL, 1> ClassA;

The above code compiles fine on MS Visual Studio 2008.
On g++ – I get the error – “a cast to a type other than an integral or enumeration type cannot appear in a constant-expression”

I tried out a few options after googling –

Declare “extern void *MY_INVALID_VAL;” in a header file – include it and do
void MY_INVALID_VAL=NULL; before template instantiation.
In that case, I get error “MY_INVALID_VAL is not a valid template argument for type ‘void
‘ because it is not a constant pointer”

So my question is –
Is there no way of instantiating a template class with NULL argument without using c++0x standard?

Thanks!

EDIT:

Thanks for all the comments and thanks for citing exact section from standards’ draft.

Just listing down the things I tried –

1) Passing “0” directly doesn’t work.
Error I get is – “could not convert ‘0’ to template argument void *”

2) Declaring static const void *my_null=0; and passing my_null doesn’t work.
It gave error – “my_null can not appear in constant expression”

3) Tried the pointer to null Object (null object pattern) approach suggested in one of the comments
See below –

class my_null
{
 public:
     my_null() { my_null_ptr = NULL; }
     void * get() { return my_null_ptr; }
 private:
    void *my_null_ptr;
};
my_null my_null_obj;
my_null *ptr = &my_null_obj;

typedef A<void *, (void *)ptr, 1> ClassA;

Still I get error – “ptr can not appear in constant expression”

So now this has me wondering – what value should I pass to make it work?
Or is there no possible to make it work? (I mean a way that does not involve using c++11 std)
I haven’t -yet- found a value that will succeed the compilation.

Any help appreciated (and needed :-P)!

As a sidenote, one more thing that I would want to ask is – is there any pointer value that I can use for non-type template argument?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:15:42+00:00Added an answer on June 11, 2026 at 11:15 pm

    C++98 says that non-type template arguments shall be one of

    • an integral constant-expression of integral or enumeration type; or
    • the name of a non-type template-parameter; or
    • the name of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as id-expression; or
    • the address of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as &id-expression where the & is option if the name refers to a function or array; or
    • a pointer to member expressed as described in 5.3.1.

    Null pointers do not fall under any item of this list, and therefore it is not valid to pass a null pointer as a non-type template parameter.

    C++11 updates this list to

    • for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or
    • the name of a non-type template-parameter; or
    • a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as & id-expression, except that the & may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
    • a constant expression that evaluates to a null pointer value (4.10); or
    • a constant expression that evaluates to a null member pointer value (4.11); or
    • a pointer to member expressed as described in 5.3.1.

    The updated requirements do cover null pointers. Therefore, to use null pointers as non-type template arguments you must use C++11.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found that Maven implies specific directory layout. But I don't understand from here:
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I'm trying to understand this line from an strace on linux: sendto(10, \24\0\0\0\26\0\1\3\233\274\362O\0\0\0\0\0\0\0\0, 20,
I try to understand this example from jquery api in this snippet var tags
i don't understand this example from the doc : the timeIntervalSinceNow method should show
This is silly, but i can't understand how to do it. Checked out from
I have some inefficiency in my app that I'd like to understand and fix.
As you can understand from title, i modified Settings.cs file.Added some properties, some code
As far as I understand from the documentation the QUdpSocket are async but, still,
what is DesignMode property? When it is useful? I don't understand it from msdn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.