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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:38:11+00:00 2026-06-17T17:38:11+00:00

Please see the code: #include <iostream> #include <typeinfo> template<int N> struct C { static

  • 0

Please see the code:

#include <iostream>
#include <typeinfo>

template<int N>
struct C
{
  static constexpr int n = N;
  using this_type_1 = C<n>;
  using this_type_2 = C<N>;
  static this_type_1* p_1;
  static this_type_2* p_2;
};

template<int N>
//C<N>* C<N>::p_1; // <--- error pattern
typename C<N>::this_type_1* C<N>::p_1; // <--- ok pattern

template<int N>
C<N>* C<N>::p_2; // ok

int main(){
  std::cerr
    << typeid(C<0>).name() << "\n"
    << typeid(C<0>::this_type_1).name() << "\n"
    << typeid(C<0>::this_type_2).name() << "\n"
  ;
}

It can compile with g++-4.7.1 and clang++-3.1. But it cannot compile with the commented-out error pattern.

g++ error message is:

test.cpp:15:13: error: conflicting declaration ‘C<N>* C<N>::p_1’
test.cpp:10:23: error: ‘C<N>::p_1’ has a previous declaration as ‘C<N>::this_type_1* C<N>::p_1’
test.cpp:15:13: error: declaration of ‘C<N>::this_type_1* C<N>::p_1’ outside of class is not definition [-fpermissive]

clang++ error message is:

test.cpp:15:13: error: redefinition of 'p_1' with a different type
C<N>* C<N>::p_1; // error
            ^
test.cpp:10:23: note: previous definition is here
  static this_type_1* p_1;
                      ^
1 error generated.

Luckily, I found out a working pattern. But I don’t know why The error pattern cannot be compiled. Please tell me the reason on the basis of the C++ language specification.

  • 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-17T17:38:13+00:00Added an answer on June 17, 2026 at 5:38 pm

    The two possible definitions for C<N>::p_1 are not as equivalent as they appear, because C<N>::n may be explicitly specialized at any time before the first instantiation for a given N.

    template<int N>
    struct C
    {
      static constexpr int n = N;
      using this_type_1 = C<n>;
      static this_type_1* p_1;
    };
    
    template<int N>
    C<N>* C<N>::p_1; // ERROR
    
    template<>
    constexpr int C<5>::n = 6;
    
    int main()
    {
        C<6>* p = C<5>::p_1;
    }
    

    If the compiler had accepted that definition of C<N>::p_1, it would be possible for its declared type to be incorrect.

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

Sidebar

Related Questions

Please see the code snippet below : #include <iostream> using namespace std; int main()
In code below (please see comment): #include stdafx.h #include <iostream> using std::cout; struct Base
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
Please see the following code: #include <iostream> #include <string> using namespace std; enum dataType
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
Please have a look at the following code #include <iostream> #include <stdlib.h> using namespace
Please consider the following code : #include <iostream> using namespace std; class superclass; class

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.