[英]C++11 “auto” to an non C++11 compiler
I have download the source from 我已从下载源
http://matt.eifelle.com/2012/07/17/just-a-small-example-of-numerical-optimization-in-c/ http://matt.eifelle.com/2012/07/17/just-a-small-example-of-numerical-optimization-in-c/
and try to compile in g++ 4.4.6 , which don't know about auto of the follwoing source : 并尝试在不了解以下源代码的g ++ 4.4.6中进行编译:
auto optimizer = Optimization::Local::build_simplex(
fun,
Optimization::Local::make_and_criteria(Optimization::Local::IterationCriterion(max_iterations),
Optimization::Local::RelativeValueCriterion<float>(ftol)));
Reading the webpage , I know it is because auto is supported in C++11 , so I try to search the source what I downloaded , there is a simplex.h which has the following source code : 阅读该网页,我知道这是因为C ++ 11支持auto,所以我尝试搜索下载的源代码,其中有一个simplex.h,其中包含以下源代码:
template<class Function, class Criterion>
static Simplex<typename Function::DataType, typename Function::ParameterType, Function, Criterion> build_simplex(const Function& fun, const Criterion& criterion)
{
return Simplex<typename Function::DataType, typename Function::ParameterType, Function, Criterion>(criterion);
}
then I change the 然后我改变
auto optimizer
to 至
Simplex<typename Function::DataType, typename Function::ParameterType, Function, Criterion> optimizer
or to 或者
Optimization::Simplex<typename Function::DataType, typename Function::ParameterType, Function, Criterion> optimizer
none would pass the compiler !! 没有人会通过编译器!
I don't know much about template usage of c++ , except for get a c++11 compile , What returned datatype modification should I do for this auto ? 我对c ++的模板用法了解不多,除了获得c ++ 11编译外,我应该为此auto做哪些返回的数据类型修改?
Update : 更新:
Thanks for kindly help, I modify to int optimizer
and compile , I got : test_rosenbrock_simplex.cpp:44: error: cannot convert Optimization::Local::Simplex<float, Eigen::Matrix<float, 2, 1, 0, 2, 1>, Rosenbrock, Optimization::Local::AndCriteria<Optimization::Local::IterationCriterion, Optimization::Local::RelativeValueCriterion<float> > >
to int
in initialization 感谢您的友好帮助,我将其修改为
int optimizer
并进行编译,得到了: test_rosenbrock_simplex.cpp:44: error: cannot convert Optimization::Local::Simplex<float, Eigen::Matrix<float, 2, 1, 0, 2, 1>, Rosenbrock, Optimization::Local::AndCriteria<Optimization::Local::IterationCriterion, Optimization::Local::RelativeValueCriterion<float> > >
初始化为int
then I change the code to : 然后我将代码更改为:
Optimization::Local::Simplex<float, Eigen::Matrix<float, 2, 1, 0, 2, 1>, Rosenbrock, Optimization::Local::AndCriteria<Optimization::Local::IterationCriterion, Optimization::Local::RelativeValueCriterion<float> > > optimizer
it works !!!!! 有用 !!!!!
g++ -O2 -march=native -msse2 -m64 -DEIGEN_NO_DEBUG test_rosenbrock_simplex.cpp -I/home/usrA/tools/eigen-eigen-5097c01bcdc4 -o test_rosenbrock_simplex.exe g ++ -O2 -march = native -msse2 -m64 -DEIGEN_NO_DEBUG test_rosenbrock_simplex.cpp -I / home / usrA / tools / eigen-eigen-5097c01bcdc4 -o test_rosenbrock_simplex.exe
./test_rosenbrock_simplex.exe ./test_rosenbrock_simplex.exe
-2.75 -0.5 0
2.1875 1.125 0
Starting point: 10
10
Starting value: 8181
Best point: 1
1
Best value: 5.68434e-14
You have to substitute in the actual template arguments, merely the declared return type doesn't cut it. 您必须替换实际的模板参数,只是声明的返回类型不会削减它。
As Drew mentioned in a comment, you can convince the compiler to tell you this. 正如Drew在评论中提到的那样,您可以说服编译器告诉您这一点。
Simply write 只需写
int optimizer = .....;
and inspect the resulting error. 并检查产生的错误。
Boost has a macro that implements a c++11 auto lookalike. Boost具有一个实现c ++ 11自动外观的宏。 It's called
BOOST_AUTO
. 它称为
BOOST_AUTO
。
It would look like: 它看起来像:
BOOST_AUTO(optimizer, Optimization::Local::build_simplex(
fun,
Optimization::Local::make_and_criteria(Optimization::Local::IterationCriterion(max_iterations),
Optimization::Local::RelativeValueCriterion<float>(ftol)));
Indeed, either you use the Boost macro, or you write the full type yourself. 确实,您可以使用Boost宏,也可以自己编写完整类型。 I'm sorry I didn't put everything, but I would code this only with a C++11 compiler.
对不起,我没有放所有东西,但是我只能用C ++ 11编译器进行编码。 Otherwise, stick with non-template code.
否则,请坚持使用非模板代码。 (I'm the blog author).
(我是博客作者)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.