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

  • Home
  • SEARCH
  • 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 6728683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:11:58+00:00 2026-05-26T10:11:58+00:00

Is this answer considered good code or is it just an ugly hack? And

  • 0

Is this answer considered “good” code or is it just an ugly hack?

And I would like to know how this is forward-declared (both classes).

When I just forward-declare the class with 2 template-parameters, it just always takes this one, no matter what value flag has.

I would like to do this because I have 2 special member functions which should behave differently on flag being true and I don’t feel like reimplementing the whole class. Also, it should have the same name. According to this example, this seems to be possible.

And I have to forward-declare it because I’m creating a library in which we forward-declare everything.

Any idea?

  • 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-26T10:11:59+00:00Added an answer on May 26, 2026 at 10:11 am

    In order to use specialisation its definition always has to be visible to the caller. If, for example, you have template <class Type, bool flag> struct something defined in one header and template <class Type> struct something<Type, true> : public something<Type, false> defined in the second one, to use the latter you have to include the second header. Without that you will always get the first, more generic type.

    EDIT: the bit about forward-declaring got me thinking. If you want to use only type declaration, as in pointer variable, do the following:

    Header

    template <class Type, bool flag>
    struct something;
    
    struct Test
    {
        something<int, true>* ptr; // definition not needed
        Test();
    }
    

    Source

    #include "something.h" // header with template
    #include "something_spec.h" // header with specialisation
    
    Test::Test()
    {
        ptr = new something<int, true>(); // specialisation used
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess no one will have a definative answer to this but considered predictions
Just wondering if this is considered a clear use of goto in C#: IDatabase
Of course, I know the best answer is don't write your own cross-platform code,
Consider such code (this is just example not real code): class Foo(url : String)
This answer says that Linq is targeted at a slightly different group of developers
This answer to a question about C++ unit test frameworks suggests a possibility that
This answer says: Vim's undo/redo system is unbeatable. Type something, undo, type something else,
This answer had me slightly confused. What is a 'select to a temp table'
This answer suggested i should put my data in JS instead of a textarea.
This came up from this answer to a previous question of mine . Is

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.