在C++命名空间中使用内置类型(double,int等)?

mfo*_*bes 0 c++ swig typedef namespaces built-in

我可以让内置的类型,如intdouble在C++命名空间中可用?

#include <complex>

typedef int my_int;

namespace my_namespace {
  namespace std = ::std;  // Works
  using ::my_int;         // Works

  using ::int;            // Fails: "expected unqualified-id before 'int'"
  typedef ::int int;      // Fails: "expected unqualified-id before 'int'"
}

typedef int out::int;     // Fails: "expected unqualified-id before 'int'"

my_namespace::my_int x;            // Works
my_namespace::std::complex<int> c; // Works

// I would like to use this:
my_namespace::int x2;    // Fails: "expected unqualified-id before 'int'" 
Run Code Online (Sandbox Code Playgroud)

我怀疑这是由于语言限制,禁止在其中使用带有关键字的限定标识符(int在本例中),但我希望有一些方法可以揭示这些.

用例

我正在尝试使用命名空间来组织类型,以便外部工具(SWIG)可以适当地用另一种语言包装函数.例如:

void one_two(int &x, int &y) { x = 1; y = 2; }
Run Code Online (Sandbox Code Playgroud)

在目标语言中,这可以包装为一个改变其参数one_two(x, y)的函数,或一个返回输出的函数x, y = one_two().我想某种方式"注释"为SWIG提供预期用法的论据.当前SWIG实现的最干净选项是使用命名空间来区分两种用法:因此输出版本x, y = one_two()可以表示为::

void one_two(out::int &x, out::int &y) { x = 1; y = 2; }
Run Code Online (Sandbox Code Playgroud)

如果我能以某种方式成为out::int同义词int.(这种方法适用于用户定义的类型.)

Bil*_*eal 5

int和朋友是该语言的关键词.它们不是全局命名空间中的名称; 就语言而言,它们根本不是名字.限定他们::是一个错误.试图命名任何变量int也是一个错误.具体来说,参见C++ 03中的2.1第1段和C++ 11中的2.12第1段(文本相同):

The identifiers shown in Table 3 are reserved for use as keywords (that is, they
are unconditionally treated as keywords in phase 7):
[ ... ]
int
Run Code Online (Sandbox Code Playgroud)