在我的真实程序中引入std :: async的嵌套调用后遇到崩溃后,我能够在下面的最小示例中重现该问题.它经常崩溃,但并非总是如此.你看到出了什么问题,还是编译器或标准库bug?请注意,如果get()添加对期货的调用,问题仍然存在.
#include <future>
#include <vector>
int main (int, char *[])
{
std::vector<std::future<void>> v;
v.reserve(100);
for (int i = 0; i != 100; ++i)
{
v.emplace_back(std::async(std::launch::async, [] () {
std::async(std::launch::async, [] { });
}));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我观察到两种不同的崩溃:(约每五次运行)
环境:
g++ -std=c++11 -pthread futures.cpp选项-pthread?
可能是因为某种原因在我的环境中,这个选项-pthread没有被考虑在内吗?无论有没有这个选项,我都会观察到相同的行为.
两者都应该在O(n log n)中运行,但一般排序比stable_sort快.实践中的性能差距有多大?你对此有一些经验吗?
我想排序大量大约20字节的结构.结果的稳定性在我的情况下会很好,但它不是必须的.目前底层容器是一个普通数组,也许稍后可以将其更改为std :: deque.
根据"C++编码标准"第32项中的描述,我有一个值类.简而言之,这意味着它提供了值语义,并且没有任何虚方法.
我不希望一个类派生自这个类.除了其他人之外,一个原因是它有一个公共的非虚拟析构函数.但是,基类应该具有公共和虚拟或受保护和非虚拟的析构函数.
我不知道写值类的可能性,因此不可能从中派生出来.我想在编译时禁止它.是否有任何已知的成语可以做到这一点?如果没有,或许在即将到来的C++ 0x中有一些新的可能性?还是有充分的理由说没有这种可能性?
我正在使用boost 1.54.0.您可以在下面找到说明我的问题的最小示例.
我使用boost日志的severity_logger.我想从流中配置我的接收器.(在下面的示例中,我使用了一个字符串流.在我的实际应用程序中,流来自一个文件.)我想使用%Severity%来输出或过滤.
我的问题是:如果我在下面的例子中给出它,%Severity%为空.
%LineID%和%Message%按预期填充.如果我在outcommented行中设置了一个接收器,它会按预期工作.
有任何想法吗?
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/expressions.hpp>
enum SeverityLevel { trace, fatal };
int main (int argc, char *argv[])
{
boost::log::add_common_attributes();
/*
struct severity_tag;
boost::log::add_console_log(std::clog,
boost::log::keywords::format = (
boost::log::expressions::stream
<< boost::log::expressions::attr< unsigned int >("LineID")
<< ": <" << boost::log::expressions::attr<SeverityLevel, severity_tag >("Severity")
<< "> " << boost::log::expressions::smessage)
); */
std::stringstream s;
s << "[Sinks.MySink]" << std::endl;
s << "Destination=Console" << std::endl;
s << "Format=\"%LineID%: <%Severity%> - %Message%\"" …Run Code Online (Sandbox Code Playgroud) 我有一个QTreeView并且想要行的不同背景颜色,具体取决于它们的内容.为实现这一目标,我从中派生了一个class MyTreeViewfrom QTreeView并实现了paint方法,如下所示:
void MyTreeView::drawRow (QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionViewItem newOption(option);
if (someCondition)
{
newOption.palette.setColor( QPalette::Base, QColor(255, 0, 0) );
newOption.palette.setColor( QPalette::AlternateBase, QColor(200, 0, 0) );
}
else
{
newOption.palette.setColor( QPalette::Base, QColor(0, 0, 255) );
newOption.palette.setColor( QPalette::AlternateBase, QColor(0, 0, 200) );
}
QTreeView::drawRow(painter, newOption, index);
}
Run Code Online (Sandbox Code Playgroud)
最初,我设置setAlternatingRowColors(true);了QTreeView.
我的问题:为QPalette :: Base设置颜色没有任何效果.每隔一行保持白色.
但是,设置QPalette :: AlternateBase按预期工作.我试着setAutoFillBackground(true)和setAutoFillBackground(false)无任何影响.
有没有提示如何解决这个问题?谢谢.
备注:通过调整 …
在ISO 8601中,持续时间以格式给出P[n]Y[n]M[n]DT[n]H[n]M[n]S.
例子:
20秒:
PT20.0S
Run Code Online (Sandbox Code Playgroud)
一年,2个月,3天,4小时,5分钟,6秒:
P1Y2M3DT4H5M6S
Run Code Online (Sandbox Code Playgroud)
题:
给定一个包含iso 8601格式持续时间的字符串.我想获得该持续时间的总秒数.标准C++ 11中推荐的方法是什么?
备注:
例如,在boost DateTime中有ptime from_iso_string(std :: string),这里不适合.没有手工制作正则表达式有类似的方法吗?
我通过说将 CMake 与 qt 一起使用:
find_package(Qt5 COMPONENTS Widgets)
Run Code Online (Sandbox Code Playgroud)
另外,我想使用高警告级别,并且我想将警告视为错误。所以我使用:
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra" )
Run Code Online (Sandbox Code Playgroud)
但是,我不关心我使用的库中的警告。因此,例如,要SYSTEM在include_directories调用中包含 boost ,我就不会受到来自外部库的警告的困扰:
include_directories(SYSTEM ${Boost_INCLUDE_DIR} )
Run Code Online (Sandbox Code Playgroud)
但这对 qt 不起作用,因为没有明确的 include_directories声明可以在前面加上SYSTEM.
有什么我可以做的吗?我只在这里找到了对该功能的请求:http : //www.itk.org/Bug/print_bug_page.php?bug_id=8710
序言:我知道,禁用警告不是一个好主意.无论如何,我对此有一个技术问题.
使用GCC 3.3.6,我收到以下警告:
choosing ... over ... because conversion sequence for the argument is better.
Run Code Online (Sandbox Code Playgroud)
现在,我想通过提供类似的参数来禁用此警告,如gcc警告选项中所述
-Wno-theNameOfTheWarning
Run Code Online (Sandbox Code Playgroud)
但我不知道警告的名称.如何找出禁用此警告的选项的名称?
我无法修复警告,因为它出现在无法更改的外部库的标头中.它在boost序列化(rx(s, count))中:
template<class Archive, class Container, class InputFunction, class R>
inline void load_collection(Archive & ar, Container &s)
{
s.clear();
// retrieve number of elements
collection_size_type count;
unsigned int item_version;
ar >> BOOST_SERIALIZATION_NVP(count);
if(3 < ar.get_library_version())
ar >> BOOST_SERIALIZATION_NVP(item_version);
else
item_version = 0;
R rx;
rx(s, count);
std::size_t c = count;
InputFunction ifunc;
while(c-- > 0){
ifunc(ar, s, …Run Code Online (Sandbox Code Playgroud)