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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:23:35+00:00 2026-05-21T07:23:35+00:00

What syntax I want to achieve on user side: double a(1.), b(2.), deps(.1); bool

  • 0

What syntax I want to achieve on user side:

double a(1.), b(2.), deps(.1);
bool res1 = compare<double>()(a, b);        // works with default eps
bool res2 = compare<double, &deps>()(a, b);  // works with explicitly provided eps
float c(1.), d(1.). feps(.1);
bool res3 = compare<float>()(c, d);  // don't have default eps - must not compile
bool res4 = compare<float, &feps>()(c, d);   // works only with provided eps

What implementation for this I have now (not working because default parameters for partial specialization are not allowed):

extern double eps_double; // somewhere defined and initialized

template<typename T, const T* eps>
struct compare { // actually inherits std::binary_function
  bool operator()(const T& t1, const T& t2) {
    return t1 < t2 - *eps;
  }
};
template<const double* eps = &eps_double>
struct compare<double, eps> { // the same as in default implementation
};

I’ve tried with enable_if and wrapper classes that have static members, but static members can’t be assigned to extern variables;

UPDATE:
The actual problem is name equality for general struct and specialized struct. I don’t know how to make it work without renaming:

// treats all explicitly passed eps and don't need default parameter
template<typename T, const T* eps>
struct compare_eps { // need another name! 
  bool operator()(const T& t1, const T& t2) {
    return t1 < t2 - *eps;
  }
};
// don't need default parameter either
// because we always know what eps to use for a concrete type
template<typename T>
struct compare { 
  // define nothing -> will not compile on types we don't have specialization for
}; 
template<>
struct compare<double> { 
  // use here eps_double hardcoded
}; 
  • 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-21T07:23:36+00:00Added an answer on May 21, 2026 at 7:23 am

    I don’t know why you think that this make sense

    compare<double, deps>
    

    You cannot make this work: Template arguments cannot be values of type double (they can be an lvalue of type double, but your template requires the address of a double, so that’s off).

    You can use function templates to make your syntax work

    extern double eps_double;
    
    template<typename T>
    types::compare<T, &eps_double> compare(
      typename enable_if<is_same<T, double>>::type * = 0
    ) {
      return types::compare<T, &eps_double>(); 
    }
    
    template<typename T, const T *eps>
    types::compare<T, eps> compare() {
      return types::compare<T, eps>(); 
    }
    

    Alternatively, you can use class templates if you are up for some ugly hacks

    template<typename T, const T* eps = &eps_double>
    struct compare { 
      bool operator()(const T& t1, const T& t2) {
        return t1 < t2 - *eps;
      }
    };
    

    The default argument will not be used if you provide both arguments. If you provide only <double>, the default argument will be used and will work. If you only provide <float>, the default argument will be used too, but will not work.

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

Sidebar

Related Questions

I want to achieve a syntax similar to this one using JS setters and
I want to assign an array using inline assembly using the AT&T syntax. I
Having a bit of trouble with the syntax where we want to call a
I am new in FLEX. I want the syntax for RemotObject. Please give me
I want to write a syntax highlighting extension for Emacs, but I Googling of
Question: I want code for: syntax highlighting (of programming languages) Language: C# or assembly
I want to use the minted package to give me syntax highlighting but it
I want to use jquery Chili plugin for syntax highlighting a piece of code
I want to come up with a language syntax. I have read a bit
I want to parse an input file that has syntax similar to c++ source.

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.