<功能> (嵌套绑定)问题与MSVC 2010 [英] <functional> (nested bind) problems with MSVC 2010
问题描述
我有以下代码(对于冗长度我很抱歉):
I have the following code (I'm sorry for the lengthiness):
double primeValue( const func1D &func,
const double lowerBound, const double upperBound,
const double pole )
{
// check bounds
if( lowerBound >= upperBound )
throw runtime_error( "lowerBound must be smaller than upperBound!" );
// C++0x way of writing: fullFunc(x) = func(x)/(x-a)
func1D fullFunc =
bind( divides<double>(), // division of
bind(func, _1), // f(x), with _1 = x
bind(minus<double>(), _1, pole) ); // by x-a, with _1 = x
// pole not in domain
if( pole<lowerBound || pole>upperBound)
{
cout << "Case 1" << endl;
return integrateSimpson( fullFunc, 1000, lowerBound, upperBound );
}
// pole closer to upper bound
else if( upperBound-pole < pole-lowerBound )
{
cout << "Case 2" << endl;
// C++0x way of writing g(x) := [f(x)-f(2a-x)]/(x-a)
func1D specialFirstFunc =
bind( std::divides<double>(), // division of
bind(minus<double>(), // numerator:
bind(func, _1), // f(x) minus
bind(func, bind(minus<double>(), 2.*pole, _1))), //f(2a-x)
bind(minus<double>(), _1, pole) ); // denominator: x-a
const double trickyPart = integrateSimpson( specialFirstFunc, 1000, pole+.000001, upperBound );
const double normalPart = integrateSimpson( fullFunc, 1000, lowerBound, 2.*pole-upperBound );
cout << "Tricky part: " << trickyPart << endl;
cout << "Normal part: " << normalPart << endl;
return trickyPart + normalPart;
}
else // pole closer to lower bound
{
cout << "Case 3" << endl;
// C++0x way of writing g(x) := [f(x)-f(2a-x)]/(x-a)
func1D specialFirstFunc =
bind( std::divides<double>(), // division of
bind(minus<double>(), // numerator:
bind(func, _1), // f(x) minus
bind(func, bind(minus<double>(), 2.*pole, _1))), //f(2a-x)
bind(minus<double>(), _1, pole) ); // denominator: x-a
const double trickyPart = integrateSimpson( specialFirstFunc, 1000, lowerBound, pole-.00001 );
const double normalPart = integrateSimpson( fullFunc, 1000, 2.*pole-lowerBound, upperBound );
cout << "Tricky part: " << trickyPart << endl;
cout << "Normal part: " << normalPart << endl;
return trickyPart + normalPart;
}
}
它集成了包含奇异性的实轴上的函数(极点)使用复杂分析的数学域的主值概念。 bind
和函数
零件将原始函数f(x)修改为
It integrates functions over the real axis that contain a singularity (pole) using the principal values concept from the math domain of Complex Analysis. The bind
and function
parts modify the original function f(x) to
(f(x)-f(2 * pole-x))/(xa)
(f(x)-f(2*pole-x))/(x-a)
$ b b
它甚至给我正确的结果,我的简单的测试用例函数。如果需要,我可以提供的其他细节:
It even gives he correct result for my simple test case function. Additional details I can provide if requested:
typedef std::function<double (double)> func1D;
double integrateSimpson( func1D, const size_t nSteps, const double lowerBound, const double upperBound);
后者使用简单的Simpson集成规则进行集成。
The latter integrates using the simple Simpson integration rule. Code can be provided, but isn't very relevant to the problem at hand.
这是用GCC 4.4+编译得很好的(用4.4.5和4.5.2 prerelease测试) ,CFLAGS = - O2 -std = c ++ 0x -pedantic -Wall -Wextra),但在MSVC 2010上生成内部标题错误(C2664)。(如果需要,可以提供错误输出,我的代码(!))。
This compiles fine with GCC 4.4+ (tested with 4.4.5 and 4.5.2 prerelease, CFLAGS="-O2 -std=c++0x -pedantic -Wall -Wextra"), but produces internal header errors (C2664) on MSVC 2010. (I can provide error output if needed, there are no references at all to my code (!)).
我在MSVC中发现了一个错误吗?
Have I found a bug in MSVC?
谢谢! >
Thanks!
推荐答案
为什么不使用lambda?
Why not just use a lambda? All of the binding stuff has been deprecated for this kind of purpose.
double primeValue( const func1D &func,
const double lowerBound, const double upperBound,
const double pole )
{
// check bounds
if( lowerBound >= upperBound )
throw runtime_error( "lowerBound must be smaller than upperBound!" );
// C++0x way of writing: fullFunc(x) = func(x)/(x-a)
auto fullFunc = [=](double d) {
return func(d) / (d - pole);
};
// pole not in domain
if( pole<lowerBound || pole>upperBound)
{
cout << "Case 1" << endl;
return integrateSimpson( fullFunc, 1000, lowerBound, upperBound );
}
// pole closer to upper bound
else if( upperBound-pole < pole-lowerBound )
{
cout << "Case 2" << endl;
// C++0x way of writing g(x) := [f(x)-f(2a-x)]/(x-a)
auto specialFirstFunc = [=](double x) -> double {
double numerator = func(x) - func(2*pole - x);
return numerator / (x - pole);
};
const double trickyPart = integrateSimpson( specialFirstFunc, 1000, pole+.000001, upperBound );
const double normalPart = integrateSimpson( fullFunc, 1000, lowerBound, 2.*pole-upperBound );
cout << "Tricky part: " << trickyPart << endl;
cout << "Normal part: " << normalPart << endl;
return trickyPart + normalPart;
}
else // pole closer to lower bound
{
cout << "Case 3" << endl;
// C++0x way of writing g(x) := [f(x)-f(2a-x)]/(x-a)
auto specialFirstFunc = [=](double x) -> double {
double numerator = func(x) - func(2*pole - x);
return numerator / (x - pole);
};
const double trickyPart = integrateSimpson( specialFirstFunc, 1000, lowerBound, pole-.00001 );
const double normalPart = integrateSimpson( fullFunc, 1000, 2.*pole-lowerBound, upperBound );
cout << "Tricky part: " << trickyPart << endl;
cout << "Normal part: " << normalPart << endl;
return trickyPart + normalPart;
}
}
至于你是否真的发现了一个错误MSVC,我不知道,但你的解决方案绝对没有必要 - 这个代码是更干净,更容易维护。
As for whether or not you actually found a bug in MSVC, I don't know, but your solution is definitely way unnecessary - this code is far cleaner and easier to maintain.
这篇关于<功能> (嵌套绑定)问题与MSVC 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!