草庐IT

namespace-scoped

全部标签

c++ - "namespace std {}"之前的 "using namespace std;"

我在许多使用STL类的地方都看到了下面的语法,而没有用std::明确限定它们。初始namespacestd{}有什么好处?为什么不直接放usingnamespacestd;?namespacestd{}usingnamespacestd; 最佳答案 namespacestd{}只是声明命名空间,以便编译器知道它并且执行usingnamespacestd;不会导致错误。std::后面的代码可以是#included并且可以在没有std::的情况下自动引用它们前缀。 关于c++-"namesp

c++ - 为什么 Boost scoped_lock 不解锁互斥锁?

我一直在以这种方式使用boost::mutex::scoped_lock:voidClassName::FunctionName(){{boost::mutex::scoped_lockscopedLock(mutex_);//dostuffwaitBoolean=true;}while(waitBoolean==true){sleep(1);}//getonwiththethread'sactivities}基本上它设置waitBoolean,而另一个线程通过将waitBoolean设置为false来表示它已完成;然而,这似乎不起作用,因为其他线程无法锁定mutex_!!我假设通过将

c++ - 在哪里放置 using namespace std;

我想知道在哪里放置usingnamespacestd;.我看到了usingnamespacestd;的代码在intmain(){}但我把它放在#include之后.我应该把它放在哪里,放在哪里有什么不同吗? 最佳答案 将其添加到函数内部会将using语句的范围仅限于该函数。你不应该把using头文件内的声明,以避免与头文件的用户发生冲突。如果您知道不会出现冲突,则将其放在文件范围内的main之上是可以的,但即使这样也可能会导致其他导入类型出现问题,并且通常在中等规模的项目中应避免使用。我尽量避免对全局命名空间的污染,但是如果我正在编

c++ - 'using' 和 'using namespace' 之间的区别

在boost库中,经常有包含库的例子:#pragmaonce#includeusingboost::property_tree::ptree;在我的整个程序中,我一直在导入这样的命名空间:#include"../MyClass.h"usingnamespaceMyClassNamespace;谁能解释一下:using和usingnamespace的区别;否定使用usingnamespace有利于using;前向声明using和usingnamespace的区别;谢谢 最佳答案 usingnamespace使命名空间的所有名称可见,而

c++ - scoped_lock 可以在读取模式下锁定 shared_mutex 吗?

C++17引入了std::shared_mutex和std::scoped_lock。我现在的问题是,当它作为参数传递时,scoped_lock将始终以独占(写入器)模式锁定共享互斥锁,而不是在共享(读取器)模式下。在我的应用程序中,我需要使用来自对象src的数据更新对象dst。我想锁定src共享和dst独占。不幸的是,如果同时调用另一个带有src和dst切换的更新方法,这可能会导致死锁。所以我想使用std::scoped_lock的花哨的死锁避免机制。我可以使用scoped_lock在独占模式下同时锁定src和dst,但是这种不必要的严格锁定会在其他地方产生性能回退。但是,似乎可以将

c++ - scoped_lock 如何避免发出 "unused variable"警告?

boost::mutex::scoped_lock是一个方便的RAII包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个RAII包装器,它要求数据接口(interface)从/重新连接到串行设备。不过,我想不通的是,为什么在下面的代码中只有我的对象mst(其实例化和销毁确实有副作用)会导致g++发出“未使用的变量”警告错误,而l设法保持沉默。你知道吗?你能告诉我吗?[generic@sentinel~]$cattest.cpp#include#include#includestructMyScopedThing;structMyWorkerObject{voida(){std:

C++ 标准 : do namespace-scoped constexpr variables have internal linkage?

假设我们有一个标题foo.h包含以下内容:#ifndefFOO_H_#defineFOO_H_namespacefoo{constexprstd::string_viewkSomeString="blah";}#endif//FOO_H_foo::kSomeString是否保证在包含foo.h的任何翻译单元中具有内部链接?这在C++11和C++17之间是否有所不同?在标准草案中[basic.link]/3说Anamehavingnamespacescopehasinternallinkageifitisthenameof[...]anon-inlinevariableofnon-vol

c++ - 匿名命名空间内的 "using namespace"语句

当在匿名命名空间中使用usingnamespace语句时,将使用的命名空间带入文件范围?例如:namespacefoo{intf(){return1;}}namespace{usingnamespacefoo;}inta(){returnf();//Willthiscompile?} 最佳答案 根据7.3.4[namespace.udir]第4段,命名空间指令是可传递的:Forunqualifiedlookupnominatesasecondnamespacethatitselfcontainsusing-directives,th

c++ - 带有 Cygwin 1.7 的 GoogleTest 1.6 编译错误 : 'fileno' was not declared in this scope

带有Cygwin1.7的GoogleTest1.6:“fileno”未在此范围内声明在EclipseCDT中对Factorial()函数进行简单测试时出现错误消息:Invoking:CygwinC++Compilerg++-std=c++0x-DGTEST_OS_CYGWIN=1-I"E:\source\gtest-1.6.0\include"-O0-g3-Wall-c-fmessage-length=0-MMD-MP-MF"src/challenge.d"-MT"src/challenge.d"-o"src/challenge.o""../src/challenge.cpp"Infi

c++ - 为什么 boost 没有 make_scoped()?

Boost的make_shared()函数promise在尝试创建shared_ptr时是异常安全的.为什么没有make_scoped()相等的?是否有通用的最佳做法?这是来自boost::scoped_ptrdocumentation的代码示例这对我来说似乎不安全:boost::scoped_ptrx(newShoe);这行代码会依次完成这三件事:为Shoe分配堆内存调用Shoe的构造函数调用boost::scoped_ptr的构造函数如果Shoe的构造函数抛出异常,内存将被泄露。(参见R.MartinhoFernandes的回答)scoped_ptr不会处理释放,因为它还没有被构造