我使用一个代码,我将枚举*转换为int*.像这样的东西:
enum foo { ... }
...
foo foobar;
int *pi = reinterpret_cast<int*>(&foobar);
Run Code Online (Sandbox Code Playgroud)
在编译代码(g ++ 4.1.2)时,我收到以下警告消息:
dereferencing type-punned pointer will break strict-aliasing rules
Run Code Online (Sandbox Code Playgroud)
我用Google搜索了这条消息,发现只有在严格的别名优化打开时才会发生这种情况.我有以下问题:
是的,我实际上需要这种别名.
我在Linux机器上,我没有root权限.我想通过CPAN将一些软件包安装到我的主目录中,这样当我运行Perl时,它就能看到它.
我跑了cpan,这要求一些配置选项.它要求提供一些目录,它建议~/perl"对于非root用户".尽管如此,当我尝试安装软件包时,它在make install步骤中失败了,因为我没有写入权限/usr/lib/perl5/whatever.
如何配置CPAN以便我可以将软件包安装到我的主目录中?
我想知道如何设置默认的编译器/链接器/等.标志,如果我使用Autoconf/Automake组合.
例如,如果我没有设置任何内容,则默认编译器标志为"-O2 -g".我可以用其他东西覆盖它,例如,如果我想调试:
./configure 'CXXFLAGS=-O0 -g'
Run Code Online (Sandbox Code Playgroud)
但我发现默认配置很愚蠢,因为如果我启用优化,调试将变得不可能.所以默认标志应该是"-O2"或"-O0 -g",如果我configure没有参数运行.我该怎么做?
编辑:我尝试了以下解决方案:
progname_CXXFLAGS=whateverMakefile.am.它不起作用,因为它将标志添加到默认标志而不是替换它们.CXXFLAGS=whateverconfigure.ac.这有效,但后来我无法覆盖它.我从Autoconf Archive下载了一个宏,我想使用它.我需要在configure.ac文件中放置什么才能使用这个宏?
我有这样一个bimap:
using MyBimap = boost::bimaps::bimap<
boost::bimaps::unordered_set_of<A>,
boost::bimaps::unordered_set_of<B>>;
Run Code Online (Sandbox Code Playgroud)
我想从静态初始化列表构造它,因为它可以为std::map:
MyBimap map{{a1, b1}, {a2, b2}, {a3, b3}};
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不起作用,因为bimap不支持初始化列表,所以我尝试了一种解决方法.Boost的文档列出了以下构造函数:
bimap();
template< class InputIterator >
bimap(InputIterator first,InputIterator last);
bimap(const bimap &);
Run Code Online (Sandbox Code Playgroud)
所以我尝试了第二个,像这样:
std::vector<std::pair<A,B>> v{{a1, b1}, {a2, b2}, {a3, b3}};
MyBimap map(v.begin(), v.end());
Run Code Online (Sandbox Code Playgroud)
它也没用.文档并不完全清楚这个构造函数期望什么样的迭代器,但显然它不仅仅是std::pair<A, B>对象的迭代器.那么这个构造函数对这种bimap的期望是什么?
我可以在bash中执行以下操作:
output=`command`
retcode=$?
Run Code Online (Sandbox Code Playgroud)
有没有办法在Perl中做同样的事情?像这样的东西:
$output=`command`
$retcode=???
Run Code Online (Sandbox Code Playgroud) 我试着编译这段代码:
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <vector>
int main() {
std::vector<int> v{
1,5,4,2,8,5,3,7,9
};
std::cout << *boost::min_element(v | boost::adaptors::transformed(
[](int i) { return -i; })) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译失败,出现以下错误消息(在长模板实例化小说之后):
/usr/local/include/boost/iterator/transform_iterator.hpp:84:26: error: use of deleted function ‘main()::<lambda(int)>::<lambda>()’
../main.cpp:12:5: error: a lambda closure type has a deleted default constructor
Run Code Online (Sandbox Code Playgroud)
我搜索了问题,并在Boost Users邮件列表存档中找到了这个问题.它建议使用#define BOOST_RESULT_OF_USE_DECLTYPE可以解决问题.我把它放在我的代码的最开头,但它仍然没有编译.错误消息的长度似乎要短得多,但最后的错误消息是相同的.我目前正在使用Boost 1.50.
这可能是什么问题?有没有办法让这项工作?
我的问题如下.我异步启动几个操作,我想继续,直到所有操作都完成.使用Boost Asio,最直接的方法如下.假设tasks某种对象容器支持一些异步操作.
tasksToGo = tasks.size();
for (auto task: tasks) {
task.async_do_something([](const boost::system::error_code& ec)
{
if (ec) {
// handle error
} else {
if (--taslsToGo == 0) {
tasksFinished();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案的问题在于它感觉就像一个解决方法.在Boost 1.54中我可以用期货来做,但我只能同步等待,这只能从与run()被调用的地方分开的线程中进行.
for (auto task: tasks) {
futures.push_back(task.async_do_something(boost::asio::use_future));
}
for (auto future: futures) {
future.wait();
}
Run Code Online (Sandbox Code Playgroud)
这个代码比前一个代码更清晰,但是我需要一个我不想要的单独的代码.我想要一些可以像这样使用的东西:
for (auto task: tasks) {
futures.push_back(task.async_do_something(boost::asio::use_future));
}
boost::asio::spawn(ioService, [](boost::asio::yield_context yield)
{
for (auto future: futures) {
future.async_wait(yield);
}
tasksFinished();
}
Run Code Online (Sandbox Code Playgroud)
有什么可以类似地使用吗?
我想知道是否set -e通过子壳传播(即子壳继承-e其父级的设置),所以我做了一些实验.我发现了一些我无法解释的奇怪结果.
首先,这是一些基本测试.他们回归了我的期望.
( true; false ) # 1
( false; true ) # 0
( set -e; false; true ) # 1
Run Code Online (Sandbox Code Playgroud)
现在我尝试了如果我在子shell中放置一个子shell会发生什么.此表达式返回1,表示它传播.
( set -e; ( false; true ) )
Run Code Online (Sandbox Code Playgroud)
然后我尝试了这些表达式.我希望他们返回1,但我发现他们返回0.
( set -e; ( true; false ); true )
( set -e; ( set -e; false; true ); true )
Run Code Online (Sandbox Code Playgroud)
为什么?在这两种情况下,内部子shell返回1,无论是否set -e传播(我在开头检查).外部子shell具有set -e,这意味着它应该在内部子shell退出后失败,但事实并非如此.有人可以解释一下吗?
c++ ×4
boost ×3
autoconf ×2
c++11 ×2
perl ×2
return-code ×2
automake ×1
bash ×1
boost-asio ×1
boost-bimap ×1
boost-range ×1
command ×1
cpan ×1
future ×1
git ×1
gitk ×1
install ×1
lambda ×1
makefile ×1
warnings ×1