我从一位同事那里收到了以下代码,使用Boost::Signal2和lambda表达式将代码分解到最低限度。它是用g++6编译的。x和g++5.4.1(后者带有参数-std=c++11)。
应该打印出来
i: 5(应为5)
使用gcc 6.4.1(或6.1.1)arm32交叉编译器(arm-cortexa15-linux-gnueabihf-g++),并在这样的系统上运行,输出是
i: 0(应为5)
其他体系结构(x86\u 64)和编译器(gcc 5.4.1)工作正常。
当我改为代码使用信号而不是插槽时,一切都正常。
我的问题是:
-
这段代码真的应该可靠地输出i:5(应该是5),还是这段代码有错误,只是意外地工作?
-
或者ARM32 gcc6编译器中是否存在错误?(gcc 5工程)
代码:
#include <exception>
#include <iostream>
#include <boost/signals2.hpp>
class LogBuffer : public std::streambuf
{
public:
LogBuffer()
{
}
char m_buf[242 - 20];
};
namespace boost
{
void assertion_failed(char const * p_expr,
char const *,
char const *, long)
{
std::cerr << "FAILED: " << p_expr << std::endl;
}
void assertion_failed_msg(char const *,
char const * msg,
char const *,
char const *, long)
{
std::cerr << "FAILED: " << msg << std::endl;
}
}
void myfunction(void)
{
{
LogBuffer b;
std::cout << "LogBuffer size: " << sizeof(LogBuffer) << std::endl;
}
int i=5;
std::cout << i << std::endl;
auto lambda = [i] { std::cerr << "i: " << i << " (should be 5)" << std::endl; };
boost::signals2::signal<void()>::slot_type slot{lambda};
slot();
}
int main(int argc, char *argv[])
{
myfunction();
}
编译和运行提供以下输出:
arm-cortexa15-linux-gnueabihf-g++ (GCC) 6.4.1 20170811
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Linux fctj-4a 4.4.109-g68c6f3c-fsm4_axm
LogBuffer size: 256
5
i: 0 (should be 5)