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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:43:26+00:00 2026-06-01T20:43:26+00:00

Given this template: template <class A> struct Something { … // members common to

  • 0

Given this template:

template <class A>
struct Something {
    ... // members common to all template instantiations for all A types 
    SpecialType member; // but not this - I want this to be conditional...
}

…I want to use “enable_if” to have the SpecialType member exist conditionally; that is, only when the template is instantiated with A=SpecialCase1 or SpecialCase2 types. In all other cases, I want the SpecialType member to be missing.

In case you’re wondering why, this is about optimization – i.e. not carrying useless payload in the struct. I am a newbie in template metaprogramming, but I understand I need “enable_if” and two “is_same” somehow – not sure exactly how, though…

EDIT: Doing it with generic C++ (i.e. without Boost-specifics) would be a plus.

  • 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-01T20:43:28+00:00Added an answer on June 1, 2026 at 8:43 pm

    Well: use a base class.

    struct Empty {};
    
    struct SpecialTypeCnt { SpecialType member; };
    
    template <typename A>
    struct Something: if_< /* cond */ , SpecialTypeCnt, Empty>::type {
    };
    

    Where if_ is defined as:

    template <typename, typename, typename E> struct if_ { typedef E type; };
    
    template <typename T, typename E>
    struct if_<std::true_type, T, E> { typedef T type; };
    

    (You can also specialize on a boolean)

    Now of course, you need to express your condition properly.


    Having said that, you should probably not use just a struct. Instead you should use a class which provides the operations that need be applied on member. Then you provide a class Null with a default behavior and a class SomeType with the behavior specific to member.

    Otherwise you’ll rewrite the condition everywhere you need to “perhaps” modify member, and it gets annoying real quick.

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

Sidebar

Related Questions

Given a class with a member template function like this one: template <typename t>
My code is similar to the one given in this thread . template<class T>
g++ compiler gives this error: expected `;' before 'it' template <typename T> class myList
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given this object class Contact { public IEnumerable<Person> People { get; } public string
Given this class public partial class Default : Page { private IRepository repo; ...
I have a defined a simple template class. In this class I define a
I have a simple container : template <class nodeType> list { public: struct node
I understand why member template functions cannot be virtual , but I'm not sure
Given the following base class: struct ValueType { String ToString(String const& format) const; };

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.