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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:28:54+00:00 2026-06-10T23:28:54+00:00

I am currently trying to write some flexible compile time mathematics library and just

  • 0

I am currently trying to write some flexible compile time mathematics library and just came across a substitution failure that I can’t manage to get rid of. Here is the problem:

First of all, I’m writing a rational class, I’ll put the only part that is needed.

template<typename T>
class rational
{
    static_assert(std::is_integral<T>::value, "Can only contain integral values.");

    public:

        constexpr rational(T numerator, T denominator);

    private:

        T _numerator;
        T _denominator;
};

And to allow the library to be flexible, I was trying to make a heavy use of SFINAE in order to restrict operator function calls to only rational-rational, rational-integral and integral-rational, but which would work whatever the integral and the underlying type of integral is. Here are the function declarations for operator+ for example:

template<typename T, typename U>
constexpr
rational<typename std::common_type<T, U>::type>
operator+(const rational<T>& lhs, const rational<U>& rhs);

template<typename T, typename U>
constexpr
typename std::enable_if<std::is_integral<U>::value, rational<typename std::common_type<T, U>::type>>::type
operator+(const rational<T>& lhs, const U& rhs);

template<typename T, typename U>
constexpr
typename std::enable_if<std::is_integral<T>::value, rational<typename std::common_type<T, U>::type>>::type
operator+(const T& lhs, const rational<U> rhs);

And here is a faulty segment of code. It does not crash because of the static_assert but presumably because of substitution failure:

constexpr auto r1 = rational<int>(1, 2);
constexpr auto r2 = rational<int>(2, 4);
static_assert(r1 + r2 == rational<int>(1, 1), "");

The error is the following (I only kept the errors without the surrounding blabla):

... required by substitution of 'template<class T, class U> constexpr typename std::enable_if<std::is_integral<T>::value, smath::rational<typename std::common_type<_Tp, _Up>::type> >::type smath::operator+(const T&, smath::rational<U>) [with T = smath::rational<int>; U = int]'
... required from here
... error: operands to ?: have different types 'smath::rational<int>' and 'int'
... required by substitution of 'template<class T, class U> constexpr typename std::enable_if<std::is_integral<U>::value, smath::rational<typename std::common_type<_Tp, _Up>::type> >::type smath::operator+(const smath::rational<T>&, const U&) [with T = int; U = smath::rational<int>]'
... required from here
... error: operands to ?: have different types 'int' and 'smath::rational<int>'

My guess was that g++ would chose the first template function that works with two rational numbers and would be ok with it. However, it seems that it still tries to apply the last two functions and fails while trying to do so. That I can’t understand. Some help would be welcomed 🙂

EDIT: It seems that having the rational constructor explicit resolves the problem, which is great. However, I’m still interested in knowing why the substitution failed that hard.

  • 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-10T23:28:56+00:00Added an answer on June 10, 2026 at 11:28 pm

    The problem is the type passed to std::enable_if<whatever, T>. Even though the substitution would fail, the argument needs to be reasonable but it isn’t. So, the use of typename std::common_type<T, U>::type doesn’t work if there is no such type for the potentially evaluated types. You’d need something else. What works is to create the substitution failure disabling the mixed integer/rational overloads in the template argument list:

    template<typename T, typename U, typename = typename std::enable_if<std::is_integral<U>::value, void>::type>
    constexpr
    rational<typename std::common_type<T, U>::type>
    operator+(const rational<T>& lhs, const U& rhs);
    
    template<typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
    constexpr
    rational<typename std::common_type<T, U>::type>
    operator+(const T& lhs, const rational<U> rhs);
    

    Right now I’m not entirely sure if this is a work-around to a gcc problem or if it necessary to do it that way.

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

Sidebar

Related Questions

I am currently trying to write some code that will accept some FTP details,
I'm currently trying to write a script that will connect to a site using
I am currently trying to write an addin for PowerPoint that whenever any PowerPoint
Currently I'm trying to write my first Python library and I've encountered the following
I am currently trying to write some unit test against my zend framework controller.
I'm trying to write some dates from one excel spreadsheet to another. Currently, I'm
I am currently trying to write a very simple app that sends an object
I'm currently trying to write a simple script that looks in a folder, and
I'm trying to write some jQuery code that will highlight the element the cursor
I am currently trying to write some automated tests within instruments and am having

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.