命名空间问题在c ++ 11? [英] namespace issues in c++11?

查看:138
本文介绍了命名空间问题在c ++ 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请解释下列事项:

  $ cat test.cpp 
#include< string&
std :: string div;
$ g ++ -c test.cpp
$ g ++ -std = c ++ 11 -c test.cpp
test.cpp:2:13:error:'std :: string div'重新声明为不同种类的符号
在/usr/include/c++/4.7.1/cstdlib:66:0包含的文件中,
来自/usr/include/c++/4.7.1/ext/string_conversions .h:37,
来自/usr/include/c++/4.7.1/bits/basic_string.h:2814,
来自/usr/include/c++/4.7.1/string:54,
从test.cpp:1:
/usr/include/stdlib.h:787:14:错误:以前的声明'div_t div(int,int)'
$

div 符号不应在 std 命名空间也用于C ++ 11模式?

解决方案


/usr/include/stdlib.h


.h 中的每个名称stdlib标头位于全局命名空间 cHEADER C ++ stdlib头会定义的相应名称,命名空间中的HEADER.h ,但也允许在全局命名空间中使用它们(因此它们只能执行

  // cHEADER 
#include< HEADER.h>

namespace std {
using :: one_name_from_HEADER;
using :: another_name_from_HEADER;
//等等...
}

$ b



§D.5[depr.c.headers]

code>


p2每个C标头,每个标头的名称格式为 name.h ,如同通过相应的 cname 头放置在标准库命名空间中的每个名称放置在全局命名空间范围内。这些名称是否首先在命名空间std的命名空间范围(3.3.6)内声明或定义,然后通过显式使用声明(7.3.3)注入到全局命名空间范围中。



p3 [示例:< cstdlib> 可以肯定地在命名空间std中提供其声明和定义。它还可以在全局命名空间中提供这些名称。标题< stdlib.h> 可以在全局命名空间中提供相同的声明和定义,与C标准一样。它还可以在命名空间std中提供这些名称。 -end example ]


正如你所看到的, code>< HEADER.h> 可以向 std 命名空间引入名称,如

  // HEADER.h 
#include< cHEADER>

使用std :: one_name_from_HEADER;
使用std :: another_name_from_HEADER;
//等等...
}

),这使得这些标题之间的整体区分,而不是无用的,真的。


Can somebody please explain the following:

$ cat test.cpp 
#include <string>
std::string div;
$ g++ -c test.cpp 
$ g++ -std=c++11 -c test.cpp 
test.cpp:2:13: error: 'std::string div' redeclared as different kind of symbol
In file included from /usr/include/c++/4.7.1/cstdlib:66:0,
                 from /usr/include/c++/4.7.1/ext/string_conversions.h:37,
                 from /usr/include/c++/4.7.1/bits/basic_string.h:2814,
                 from /usr/include/c++/4.7.1/string:54,
                 from test.cpp:1:
/usr/include/stdlib.h:787:14: error: previous declaration of 'div_t div(int, int)'
$

Shouldn't the div symbol be in std namespace also for C++11 mode? Or is it something specific to my system?

解决方案

/usr/include/stdlib.h

Every name in a .h C stdlib header resides in the global namespace (obviously).

Additionally, any cHEADER C++ stdlib header will define the corresponding names from HEADER.h in the std namespace, but is also allowed to have them in the global namespace (so they can just do

// cHEADER
#include <HEADER.h>

namespace std{
using ::one_name_from_HEADER;
using ::another_name_from_HEADER;
// and so on...
}

and be done with it).

§D.5 [depr.c.headers]

p2 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 global namespace scope by explicit using-declarations (7.3.3).

p3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

As you can see, the same is also true the other way around (<HEADER.h> may introduces names to the std namespace, as if

// HEADER.h
#include <cHEADER>

using std::one_name_from_HEADER;
using std::another_name_from_HEADER;
// and so on...
}

), which makes the whole distinction between those headers rather... useless, really.

这篇关于命名空间问题在c ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆