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 1108359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:04:15+00:00 2026-05-17T02:04:15+00:00

As i understood, template value need to be known in compilation time. so i

  • 0

As i understood, template value need to be known in compilation time.
so i write a little example just to see that i get it, but apperantly i didn’t.
so i get this:

`defValue' cannot appear in a constant-
 expression

can anyone please what is the problem and how it can be fixed?

#include <iostream>
template <class T,T defaultVal, int dim=255>
class Vec
{
    T _vec[dim];
    int _dim;
    public:
    Vec () : _dim(dim)
    {
       for (int i=0;i<_dim;++i)
       {
         _vec[i] = defaultVal;
       }
    }
    ~Vec () {};
// other operators and stuff
};

int main ()
{
    int defValue = 0;
    Vec < int,defValue > vecWithDefVal;// here is the problem but i don't know why
}
  • 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-05-17T02:04:16+00:00Added an answer on May 17, 2026 at 2:04 am

    C++03 14.3.2

    Template non-type arguments

    A template-argument for a non-type, non-template template-parameter shall be one of:

    — an integral constant-expression of integral or enumeration type; or

    — the name of a non-type template-parameter; 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
    optional if the name refers to a function or array, or if the corresponding template-parameter is a reference; or
    — a pointer to member expressed as described in 5.3.1 .

    defValue [being a non-type argument] is neither of the above.

    Change int defValue = 0; to const int defValue = 0 and your code will compile [see the first condition].

    Or directly write Vec <int,0> vecWithDefVal;

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

Sidebar

Related Questions

OK thought I understood IDipose but just reading the best practices section of Accelerated
I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I thought I understood Java generics pretty well, but then I came across the
How do you show your clients/employers that you understood their requirements? What do you
In code: template<class T> struct is_builtin { enum {value = 0}; }; template<> struct
In C# (within the context of T4 template see http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/ ) I want to
I haven't completely understood, how to use sigprocmask() . Particularly, how the set and
I thought I understood what the default method does to a hash... Give a
After reading Practical Common Lisp I finally understood what the big deal about macros
There is one aspect of TDD which I never fully understood. Suppose somebody asked

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.