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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:00:40+00:00 2026-06-18T01:00:40+00:00

In template meta programming, one can use SFINAE on the return type to choose

  • 0

In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e.

 template<int N> struct A {
   int sum() const noexcept
   { return _sum<N-1>(); }
 private:
   int _data[N];
   template<int I> typename std::enable_if< I,int>::type _sum() const noexcept
   { return _sum<I-1>() + _data[I]; }
   template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept
   { return _data[I]; }
};

However, this won’t work if the function in question (_sum() in above example) has auto-detected return type, such as _func() in this example

template<int N> class A
{
   /* ... */
private:
  // how to make SFINAE work for _func() ?
  template<int I, typename BinaryOp, typename UnaryFunc>
  auto _func(BinaryOp op, UnaryFunc f) const noexcept -> decltype(f(_data[0]))
  { return op(_func<I-1>(op,f),f(_data[I])); }
};

What else can be done to get SFINAE here?

  • 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-18T01:00:41+00:00Added an answer on June 18, 2026 at 1:00 am

    Following David Rodríguez – dribeas, the following code worked as intended:

    template<int N> class A
    {
      int min_abs() const noexcept
      {
        return _func<N-1>([](int x, int y)->int { return std::min(x,y); },
                          [](int x)->int { return std::abs(x); });
      }
    private:
      int _data[N];
    
      template<int I, typename BinaryOp, typename UnaryFunc>
      auto _func(BinaryOp op, UnaryFunc f) const noexcept
        -> typename std::enable_if< I>,decltype(f(_data[0]))>::type
      { return op(_func<I-1>(op,f),f(_data[I])); }
    
      template<int I, typename BinaryOp, typename UnaryFunc>
      auto _func(BinaryOp op, UnaryFunc f) const noexcept
        -> typename std::enable_if<!I>,decltype(f(_data[0]))>::type
      { return f(_data[I]); }
    };
    

    strictly speaking, we must also ensure that the binary operator op returns the correct type. For simplicity and brevity of the answer, I leave that for the reader to figure out …

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

Sidebar

Related Questions

In template meta programming, one can use SFINAE on the return type to choose
Is it possible to use member function pointers with template meta-programming? Such as: class
Here's an example from wikipedia that displays C++ template meta-programming: template <int N> struct
Is it possible to use template meta-programming to convert any struct or class into
I am documenting some code which uses meta-programming heavily, for example: template<rysq::type A, rysq::type
I wonder whether some template meta-programming facility allows one to assign unique integral identifiers
I have inherited a project making heavy use of template meta programming, and am
Is it possible to create a template meta construction that can take one execution
I want to get into more template meta-programming. I know that SFINAE stands for
I am trying to use template meta-programming to determine the base class. Is there

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.