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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:06:08+00:00 2026-05-23T17:06:08+00:00

I have a class with a template parameter which should decide which of two

  • 0

I have a class with a template parameter which should decide which of two styles of data it contains. Based on that parameter I want to implement a member function one of two different ways. I tried using Boost Enable-If, but without success. Here’s the version of the code that I’m most surprised doesn’t work:

#include <boost/utility/enable_if.hpp>
enum PadSide { Left, Right };
template <int> struct dummy { dummy(int) {} };

template <PadSide Pad>
struct String
{
    typename boost::enable_if_c<Pad ==  Left, void>::type
        getRange(dummy<0> = 0) {}
    typename boost::enable_if_c<Pad == Right, void>::type
        getRange(dummy<1> = 0) {}
};

int main()
{
    String<Left> field;
    field.getRange();
}

To this, g++ 4.6.0 says:

no type named ‘type’ in ‘struct boost::enable_if_c<false, void>’

Of course, the second overload is supposed to not work, but it’s supposed to be ignored due to SFINAE. If I remove the dummy function parameters, g++ says this:

‘typename boost::enable_if_c<(Pad == Right), void>::type
    String<Pad>::getRange()‘
cannot be overloaded with
‘typename boost::enable_if_c<(Pad == Left), void>::type
    String<Pad>::getRange()‘

Which is why I put the dummy parameters there in the first place–following the Compiler Workarounds section of the documentation.

Basically what I want is to have two implementations of getRange(), and have one or the other be selected based on the Pad type. I was hoping that Enable-If would let me do it without making auxiliary classes to delegate the work to (which I’m going to try in the meantime).

  • 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-23T17:06:08+00:00Added an answer on May 23, 2026 at 5:06 pm

    Since you are going to be making two different versions of getRange() anyways, you can always overload your struct String member functions depending on the type of PadSide. I know it’s not as “pretty”, but in the end, it’s still a similar amount of code, and you won’t have to make multiple class types.

    template<PadSide Pad>
    struct String
    {
        void getRange();
    };
    
    template<>
    void String<Right>::getRange() { /*....*/ }
    
    template<>
    void String<Left>::getRange() { /*....*/ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class that I serialize (call it C), for which I
I have a template class where I want to use objects of that class
Is there a way to have the compile deduce the template parameter automatically? template<class
I have a template class that is only valid for couple of template parameters:
I have a C++ template class that gets instantiated with 3 different type parameters.
I have a templated class template <typename Data> class C { ..... } In
I have a template class for which I need to access a protected member
I have a template class like below. template<int S> class A { private: char
I have a template class for thread-safe vector: template <class T> class SharedVector {
Let's say we have a class, MyParent: class MyParent { public: template<namespace T> MyParent()

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.