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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:40:51+00:00 2026-06-02T16:40:51+00:00

I ran into some trouble trying to implement a smart equality test macro-type template

  • 0

I ran into some trouble trying to implement a smart equality test macro-type template function in Visual C++ 2010 that had to do with a bug in VS in regard to default arguments of template functions. I fixed it by wrapping the value of the parameter in an extra function, but now I found that I can’t use the function twice in one line!

Header file:

// example.h
#pragma once

#include <limits>

namespace myspace
{

// Need to define this separately to avoid a Visual Studio bug
template<typename T> T epsilon() { return std::numeric_limits<T>::epsilon(); }

// A generic equality test
template<typename T> inline bool smartEqual(
    const T &v1, 
    const T &v2, 
    const T &eps = epsilon<T>())
{
    return (v1 == v2);
}

// Template specialization for floating-point numbers
template<> bool smartEqual<float>(
    const float &v1, 
    const float &v2, 
    const float &eps);

} // namespace myspace

Source file:

// example.cpp
#include "example.h"

using namespace std;
using namespace myspace;

// equal-macro specialization for floats using epsilon
template<> bool myspace::smartEqual<float>(
    const float &v1, 
    const float &v2, 
    const float &eps)
{
    return (fabs(v1 - v2) < eps);
}

int _tmain(int argc, _TCHAR* argv[])
{
    float a,b;
    bool x = smartEqual(a,b); // works ok
    bool x = smartEqual(a,b) && smartEqual(b,a); // error
    return 0;
}

The error is reported as follows:

—— Build started: Project: test, Configuration: Debug Win32 ——
test.cpp
c:\users\ninja\documents\visual studio 2010\projects\test\test\test.cpp(24): error C2440: ‘default argument’ : cannot convert from ‘const float *’ to ‘const float &’
Reason: cannot convert from ‘const float *’ to ‘const float’
There is no context in which this conversion is possible

The offending line is the one where I try to call smartEqual() twice using the logical AND.

I don’t understand why this happens. Changing “eps” from a reference type to a straightforward value type fixes it, but I wish I knew what was going on.

Thanks!

  • 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-02T16:40:53+00:00Added an answer on June 2, 2026 at 4:40 pm

    I think you’ve now hit this VS10 bug.

    Your code compiles OK on VS11 Beta.

    You could possibly avoid the default value (which seems to be a major issue for VS10) by changing smartEqual to:

    template<typename T> inline bool smartEqual(
        const T &v1, 
        const T &v2)
    {
        return (v1 == v2);
    }
    

    and simply specialising for float (and double) like this:

    template<> bool myspace::smartEqual<float>(
        const float &v1, 
        const float &v2)
    {
        return (fabs(v1 - v2) < std::numeric_limits<float>::epsilon());
    }
    

    Another option is to change the epsilon parameter to pass by value:

    template<typename T> inline bool smartEqual(
        const T &v1, 
        const T &v2, 
        T eps = epsilon<T>())
    {
        return (v1 == v2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ran into this error message while trying to select some records off a table.
I'm trying to create some HtmlHelper extensions and ran into a bit of a
I'm writing my own software rasterizer in Java, and I ran into some trouble
I ran into some trouble while using a list of arrays. So to clear
While working on a mapping structure for our applications we ran into some trouble
I was converting some .NET-3.5-based projects to .NET Framework 4.0 and ran into trouble
I've ran into some issues using ^blocks in Objective-C. I'm trying to set an
I'm new to jsp and have ran into some trouble. Initially, the jsp file
I'm trying to learn Django and I've ran into some confusing points. I'm currently
Very new to PHP and ran into some trouble and was wondering if anyone

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.