草庐IT

variable-declaration

全部标签

C++ 警告 : anonymous type with no linkage used to declare variable

编译(gcc4.6.3,ubuntu)示例时看到此警告消息:struct{}a;intmain(){}warning:anonymoustypewithnolinkageusedtodeclarevariable‘a’withlinkage[enabledbydefault].GCC没有给出这个警告。只有G++可以。添加static清除警告:staticstruct{}a;我不明白它是什么意思,特别是为什么type与linkage相关。我认为链接取决于变量的声明位置和方式,而不取决于变量本身的类型。 最佳答案 这意味着变量a具有链接

c++ - 如何使用 condition_variable 来真正 wait_for 不超过一定的持续时间

作为itturnsout,condition_variable::wait_for真的应该叫condition_variable::wait_for_or_possibly_indefinitely_longer_than,因为它需要在真正超时和返回之前重新获取锁。见thisprogram进行演示。有没有办法表达,“看,我真的只有2秒。如果当时myPredicate()仍然是假的和/或锁是仍然被锁定,我不在乎,只要继续,给我一个方法来检测它。”类似:boolmyPredicate();autosec=std::chrono::seconds(1);boolpred;std::condi

c++ - 如何使用 condition_variable 来真正 wait_for 不超过一定的持续时间

作为itturnsout,condition_variable::wait_for真的应该叫condition_variable::wait_for_or_possibly_indefinitely_longer_than,因为它需要在真正超时和返回之前重新获取锁。见thisprogram进行演示。有没有办法表达,“看,我真的只有2秒。如果当时myPredicate()仍然是假的和/或锁是仍然被锁定,我不在乎,只要继续,给我一个方法来检测它。”类似:boolmyPredicate();autosec=std::chrono::seconds(1);boolpred;std::condi

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember

C++11 std::condition_variable:我们可以将锁直接传递给通知线程吗?

我正在学习C++11并发,我之前唯一的并发原语经验是在六年前的操作系统课上,所以如果可以的话,请保持温和。在C++11中,我们可以写std::mutexm;std::condition_variablecv;std::queueq;voidproducer_thread(){std::unique_locklock(m);q.push(42);cv.notify_one();}voidconsumer_thread(){std::unique_locklock(m);while(q.empty()){cv.wait(lock);}q.pop();}这很好用,但我对将cv.wait包装在

C++11 std::condition_variable:我们可以将锁直接传递给通知线程吗?

我正在学习C++11并发,我之前唯一的并发原语经验是在六年前的操作系统课上,所以如果可以的话,请保持温和。在C++11中,我们可以写std::mutexm;std::condition_variablecv;std::queueq;voidproducer_thread(){std::unique_locklock(m);q.push(42);cv.notify_one();}voidconsumer_thread(){std::unique_locklock(m);while(q.empty()){cv.wait(lock);}q.pop();}这很好用,但我对将cv.wait包装在

c++ - 元编程 : Declare a new struct on the fly

是否可以即时声明新类型(空结构体或没有实现的结构体)?例如constexprautomake_new_type()->???;usingA=decltype(make_new_type());usingB=decltype(make_new_type());usingC=decltype(make_new_type());static_assert(!std::is_same::value,"");static_assert(!std::is_same::value,"");static_assert(!std::is_same::value,"");“手动”解决方案是template

c++ - 元编程 : Declare a new struct on the fly

是否可以即时声明新类型(空结构体或没有实现的结构体)?例如constexprautomake_new_type()->???;usingA=decltype(make_new_type());usingB=decltype(make_new_type());usingC=decltype(make_new_type());static_assert(!std::is_same::value,"");static_assert(!std::is_same::value,"");static_assert(!std::is_same::value,"");“手动”解决方案是template

c++ - "int (x), 1;"是一个模棱两可的陈述吗?

voidf(intx){int(x),1;}Clang编译它,GCC没有。哪个编译器是正确的? 最佳答案 IMO,[stmt.ambig]中的措辞这一点很清楚:Anexpression-statementwithafunction-styleexplicittypeconversionasitsleftmostsubexpressioncanbeindistinguishablefromadeclarationwherethefirstdeclaratorstartswitha(.Inthosecasesthestatementis