为什么在一行中使用这个C ++函数两次会导致编译错误? [英] Why does using this C++ function twice in one line cause a compile error?

查看:279
本文介绍了为什么在一行中使用这个C ++函数两次会导致编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些麻烦,试图在Visual C ++ 2010中实现一个智能平等测试宏类型模板函数,它与一个错误。我修复它通过将参数的值包装在一个额外的函数,但现在我发现我不能在一行中使用该函数两次!

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!

头文件:

// 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

源文件:

// 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;
}

错误报告如下:


------ Build started:Project:test,Configuration:Debug Win32 ------

test.cpp

c:\users\\\
inja\documents\visual studio 2010\projects\test\test\test.cpp(24):错误C2440:'default argument':无法从'const float *'to'const float&'

原因:无法从'const float *'转换为'const float'

没有可进行此转换的上下文 p>

------ 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

冒犯行是我尝试使用逻辑AND调用smartEqual()两次。

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

我不明白为什么会发生这种情况。将eps从引用类型更改为直接值类型会修复它,但我希望我知道发生了什么。

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.

谢谢!

推荐答案

我想你现在已经点击此VS10错误

您的代码在VS11测试版上编译成功。

Your code compiles OK on VS11 Beta.

您可以通过更改<$ c来避免默认值(这似乎是VS10的主要问题) $ c> smartEqual 至:

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());
}



另一个选项是更改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);
}

这篇关于为什么在一行中使用这个C ++函数两次会导致编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆