Is `using namespace foo;` useless?

Suppose I have a library that places all its declarations in namespace foo . I can then conveniently use the library by adding: using namespace foo; to my source files. However, if any innocent name in foo (for example class foo::socket ) is now ambiguous with global namespace names (for example ::socket() ). What's more, there is no way to disambiguate them! Looks like using namespace

'使用namespace foo;`没用?

假设我有一个将所有声明放在名称空间foo 。 我可以通过添加以下方法方便地使用该库: using namespace foo; 到我的源文件。 但是,如果foo任何无辜名称(例如class foo::socket )现在与全局名称空间名称(例如::socket() ) ::socket() 。 更重要的是,没有办法消除它们的歧义! 看起来像using namespace只对不与全局名称空间冲突的库有用,还是我缺少某些东西? 编辑:通过“消除歧义”我的意思是一次的范围,而不是在每

Namespace questions: forward declaration and mixing namespaces

I'm not very familiar with C++, and this is my first time working with more than just the namespace std. What is the difference between the following? using MyNameSpace::MyClass; vs namespace MyNameSpace {class MyClass;} After both, it seems I can now create a MyClass object. Is one way better than the other? Also, if I don't do either, can I still reference MyClass by appending My

命名空间问题:向前声明和混合命名空间

我对C ++不是很熟悉,这是我第一次使用不仅仅是名字空间std。 以下是什么区别? using MyNameSpace::MyClass; VS namespace MyNameSpace {class MyClass;} 在两者之后,似乎我现在可以创建一个MyClass对象。 比其他方式更好吗? 另外,如果我不这样做,每次我需要时,我还可以通过在MyNameSpace :: MyClass之前添加MyClass来引用MyClass吗? 接下来,如果我使用第二个选项转发声明,是否还需要#include "MyClass.h

Legacy standard C library headers and overloaded C++ functions

C++ language standard says in D.5 2 Every C header, each of which has a name of the form name.h , behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the glo

传统的标准C库标题和重载的C ++函数

C ++语言标准在D.5中说过 2每个C头都有一个name.h形式的name.h ,就好像每个名称放置在标准库名称空间中的相应的cname头都放在全局名称空间范围内一样。 没有指定这些名称是首先在名称空间std的名称空间范围(3.3.6)中声明还是定义的,然后通过显式使用声明(7.3.3)将其注入到全局名称空间作用域中。 3 [示例:头文件<cstdlib>确实在命名空间std提供了它的声明和定义。 它也可能在全局命名空间中提供这些名称。

Does using namespace cause name hiding?

Consider the following code: namespace C { class X {}; } namespace A { class X {}; namespace B { using namespace C; X x; } } I was expecting the type of x to be C::X due to the using namespace directive, but instead both VS2010 and online LLVM/Clang compiler resolve X within the namespace B to be A::X . Changing the using directive with a using declaration (

使用名称空间会导致名称隐藏吗?

考虑下面的代码: namespace C { class X {}; } namespace A { class X {}; namespace B { using namespace C; X x; } } 由于using namespace指令,我期待x的类型是C::X ,但是VS2010和在线LLVM / Clang编译器都会将名称空间B X解析为A::X 使用using声明( using C::X )更改using指令,然后按预期方式解析为C::X 该标准说明使用指令[7.3.4.2]: using-directive指定指定名称空间中的

Can types in `cname` and `name.h` be different types?

Is this code standard conforming? #include <stdio.h> #include <cstdio> int main() { FILE *f1 = 0; std::FILE *f2 = f1; } Explanation: The standard says [headers]: [...] the contents of each header cname shall be the same as that of the corresponding header name.h [...] as if by inclusion. In the C++ standard library, however, the declarations [...] are within namespace sc

`cname`和`name.h`中的类型可以是不同的类型?

这个代码标准是否符合? #include <stdio.h> #include <cstdio> int main() { FILE *f1 = 0; std::FILE *f2 = f1; } 说明:标准说[头]: [...]每个标题cname的内容应与相应标题name.h [...]的内容相同,如同包含一样。 然而,在C ++标准库中,声明[...]在命名空间std的命名空间范围(3.3.6)内。 没有说明这些名称是否首先在全局命名空间范围内声明,然后通过显式使用声明(7.3.3)注入命名空间std

cstdio stdio.h namespace

I see this line from the c++ reference for cstdio : Every element of the library is defined within the std namespace . but I tried the code: std::printf("hello world"); printf("hello world"); is it true that C++ headers puts the names in both the std and the global namespace ? Including cstdio imports the symbol names in std namespace and possibly in Global namespace. Including stdio.h

cstdio stdio.h命名空间

我从cstdio的c ++参考中看到这一行: 库的每个元素都在std namespace定义。 但我试过这个代码: std::printf("hello world"); printf("hello world"); C ++头文件是否将名称放在std和全局名称空间中都是真的吗? 包括cstdio将标记名称导入到std命名空间中,并可能导入到Global命名空间中。 包括stdio.h在Global命名空间和可能的std命名空间中导入符号名称。 这同样适用于所有c风格的标题。 参考: C ++ 11标准

How to convert a std::string to const char* or char*?

如何将std::string转换为char*或const char* ? If you just want to pass a std::string to a function that needs const char* you can use std::string str; const char * c = str.c_str(); If you want to get a writable copy, like char * , you can do that with this: std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = ''; // don

如何将std :: string转换为const char *或char *?

如何将std::string转换为char*或const char* ? 如果你只是想将一个std::string传递给需要const char*的函数,你可以使用 std::string str; const char * c = str.c_str(); 如果你想得到一个可写的拷贝,比如char * ,你可以这样做: std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = ''; // don't forget the terminating 0 // don't fo

set is not member of boost::PolygonSet

I am trying to overload & operator for PolygonSet but end with following error. I can understand boost::PolygonSet does not have set function but can anyone provide me input to fix this issue. I have tried view_as but did not success. The code: Function definition for concept namespace boost { namespace polygon { template <> struct geometry_concept<bo

set不是boost :: PolygonSet的成员

我正在尝试为PolygonSet重载&运算符,但以下列错误结束。 我可以理解boost :: PolygonSet没有设置功能,但任何人都可以提供我解决此问题的输入。 我尝试过view_as,但没有成功。 代码: Function definition for concept namespace boost { namespace polygon { template <> struct geometry_concept<boost_geom_api::PolygonSet> { typedef polygon_set_concept typ

std::bind a bound function

I'm having trouble in detecting why the heck is this not compiling. I've got some lambda function that returns a std::function based on some argument. I've narrowed down my problem to this snippet(which doesn't use lambdas, but reproduces my error perfectly): #include <functional> #include <iostream> struct foo { template<class T> void bar(T data) {

std ::绑定一个绑定函数

我在检测为什么这不是编译时遇到了麻烦。 我有一些lambda函数返回基于某个参数的std::function 。 我已经缩小了我的问题到这个片段(它不使用lambdas,但完美地重现了我的错误): #include <functional> #include <iostream> struct foo { template<class T> void bar(T data) { std::cout << data << "n"; } }; void some_fun(const std::function<void(int)>

Compiling with gcc fails if using lambda function for QObject::connect()

This Code compiles without any problems Qt5.2.0 MSVC2012: #include <QCoreApplication> #include <QObject> #include <QTimer> #include <QDebug> int main(int argc, char *argv[]) { QTimer timer; QCoreApplication a(argc, argv); QObject::connect(&timer, &QTimer::timeout, [=] () { qDebug() << Q_FUNC_INFO; }); timer.start(100

如果使用QObject :: connect()的lambda函数编译gcc失败

此代码编译没有任何问题Qt5.2.0 MSVC2012: #include <QCoreApplication> #include <QObject> #include <QTimer> #include <QDebug> int main(int argc, char *argv[]) { QTimer timer; QCoreApplication a(argc, argv); QObject::connect(&timer, &QTimer::timeout, [=] () { qDebug() << Q_FUNC_INFO; }); timer.start(1000); return a