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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:58:58+00:00 2026-06-17T04:58:58+00:00

Given POD structures of the general form struct case_0 { const char *foo; };

  • 0

Given POD structures of the general form

struct case_0   { const char *foo;                       };
struct case_1i  { const char *foo; int v0;               };
struct case_1d  { const char *foo; double v0;            };
struct case_2ii { const char *foo; int v0;    int v1;    };
struct case_2id { const char *foo; int v0;    double v1; };
// etc

is it possible to dispatch to (template) members of a function overload set based on the presence or absence of the v0, v1, etc data members — ideally, without any dependence on the specific type of these members — and if so, how? Concretely, given

void
process(const case_0& c)
{
   do_stuff_with(c.foo);
}

template <typename case_1> void   
process(const case_1& c)
{
   do_stuff_with(c.foo, c.v0);
}

template <typename case_2> void
process(const case_2& c)
{
   do_stuff_with(c.foo, c.v0, c.v1);
}

I would like each overload to be selected for all case_* structures that have all the v-members that are used within its body, and — equally important — don’t have any v-members that are not used within its body.

This program must be 100% self-contained, so no Boost, please. C++11 features are okay.

  • 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-17T04:58:59+00:00Added an answer on June 17, 2026 at 4:58 am

    You need to write a set of traits such as has_v0 and has_v1 (which I’m sure has been demonstrated many times on SO) then constrain your overloads using them:

    template <typename case_0,
      typename = typename std::enable_if<!has_v0<case_0>::value>::type,
      typename = typename std::enable_if<!has_v1<case_0>::value>::type
    >
    void
    process(const case_0& c)
    {
       do_stuff_with(c.foo);
    }
    
    template <typename case_1,
      typename = typename std::enable_if<has_v0<case_1>::value>::type,
      typename = typename std::enable_if<!has_v1<case_1>::value>::type
    >
    void   
    process(const case_1& c)
    {
       do_stuff_with(c.foo, c.v0);
    }
    
    template <typename case_2,
      typename = typename std::enable_if<has_v0<case_2>::value>::type,
      typename = typename std::enable_if<has_v1<case_2>::value>::type
    >
    void
    process(const case_2& c)
    {
       do_stuff_with(c.foo, c.v0, c.v1);
    }
    

    You can simplify the constraints with something like

    template<typename Cond>
      using Require = typename std::enable_if<Cond::value>::type;
    

    e.g.

    template <typename case_2,
      typename = Require<has_v0<case_2>>,
      typename = Require<has_v1<case_2>>
    >
    void
    process(const case_2& c)
    {
       do_stuff_with(c.foo, c.v0, c.v1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the code in Visual Studio 2010: void Foo::Bar() const { map_t::const_iterator iter(my_map_.find(key)); if(my_map_.end()
given the controller function ctl($scope, $http) { $scope.postForm = function() { console.log(submitting form) }
Given a class declaration class A { template <typename T> T foo(); }; I
Given a JS literal object like: var foo = { _stuff : { a:10,
Given a variable foo of type FooClass* and a member variable in that class
Given an array that gets populated with strings. I need following behavior: foo =
Given the following sample code: #include <iostream> #include <memory> using namespace std; struct A
Given that the web application doesn't have su privileges, I'd like to execute a
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
Given an NSMutableArray of dynamic CGPoint s, what is the fastest and most efficient

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.