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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:41:54+00:00 2026-05-11T17:41:54+00:00

Can somebody explain to me why the following works: template<class T> class MyTemplateClass {

  • 0

Can somebody explain to me why the following works:

template<class T> class MyTemplateClass {
public:
    T * ptr;
};

int main(int argc, char** argv) {
    MyTemplateClass<double[5]> a;
    a.ptr = new double[10][5];
    a.ptr[2][3] = 7;
    printf("%g\n", a.ptr[2][3]);
    return 0;
}

But this doesn’t:

class MyClass {
public:
    double[5] * ptr;
    // double(*ptr)[5]; // This would work
};

int main(int argc, char** argv) {
    MyClass a;
    a.ptr = new double[10][5];
    a.ptr[2][3] = 7;
    printf("%g\n", a.ptr[2][3]);
    return 0;
}

Obviously there is more to template instantiation than just a textual replacement by the arguments to the template – is there a simple explanation of this magic?

For the latter the compiler (g++ 4.1.2) spits out the following error:

test.cxx:13: error: expected unqualified-id before '[' token

Where line 13 is the double[5] * ptr; line.

The question is not:

“Why does the MyClass example fail? – because C++ doesn’t allow Java style array declarations ;-)”.

But is:

“Why does the MyTemplateClass example succeed?”

  • 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-11T17:41:54+00:00Added an answer on May 11, 2026 at 5:41 pm

    The difference lies in the C++ grammar. A simple-declaration is formed like this:

    declaration-specifier-seq init-declarator-list
    

    Where declaration-specifier-seq is a sequence of declaration specifiers:

    simple-type-specifier: int, bool, unsigned, typedef-name, class-name ...
    class-specifiers: class X { ... }
    type-qualifier: const, volatile
    function-specifier: inline, virtual, ... 
    storage-class-specifier: extern, static, ...
    typedef
    

    You get the idea. And init-declarator-list is a list of declarators with an optional initializer for each:

    a
    *a
    a[N]
    a()
    &a = someObj
    

    So a full simple-declaration could look like this, containing 3 declarators:

    int a, &b = a, c[3] = { 1, 2, 3 };
    

    Class members have special rules to account for the different context in which they appear, but they are very similar. Now, you can do

    typedef int A[3];
    A *a;
    

    Since the first uses the typedef specifier and then simple-type-specifier and then a declarator like “a[N]”. The second declaration then uses the typedef-name “A” (simple-type-specifier) and then a declarator like “*a”. However, you of course cannot do

    int[3] * a;
    

    Since “int[3]” is not a valid declaration-specifier-seq as shown above.

    And now, of course, a template is not just like a macro text substitution. A template type parameter of course is treated like any other type-name which is interpreted as just the type it names and can appear where a simple-type-specifier can appear. Some C# folks tend to say C++ templates are “just like macros”, but of course they are not 🙂

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

Sidebar

Related Questions

I came across the following program class Boolean { public static void main(String argv[])
Could somebody explain how the following example works? I don't understand how this can
Can somebody explain why the following code is not valid? Is it because the
Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For
Can somebody explain the following code please? #if 1 // loop type #define FOR_IS_FASTER
Can somebody explain me why the following piece of code fails to compile. The
Can Somebody explain me on short (just as idea) what the following fragment suggests?
Can somebody explain to me the following (python 2.7) Two string numbers, from a
Can somebody explain to me why classes are not first class objects in Java?
can somebody explain why is the following trivial code (implementation of Euclid's algorithm to

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.