为什么语句不能出现在命名空间范围内?

Why statements cannot appear at namespace scope?(为什么语句不能出现在命名空间范围内?)

本文介绍了为什么语句不能出现在命名空间范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于标准中哪条规则的任何想法都是这样表述的:

Any idea on which rule in standard states the statements like this:

p++; //where 'p' is pointer to array

不能出现在全局范围内?

cannot appear in global scope?

如果可能的话,我正在寻找参考,而不仅仅是解释.

I'm looking for a reference not just an explanation if possible.

推荐答案

您编写的表达式 p++ 在命名空间范围内.namespace-body 的语法禁止它在 §7.3.1/1 中定义为:

The expression p++ which you've written is at namespace scope. It is forbidden by the grammer of namespace-body which is defined in §7.3.1/1 as:

命名空间主体:
    声明-seqopt

表示命名空间主体可以可选地包含声明.而 p++ 肯定不是一个声明,它是一个表达式,因此标准隐含地禁止它.标准可能有明确的声明禁止这样做,但我认为以上应该足够了.

which says the namespace-body can optionally contain only declaration. And p++ is surely not a declaration, it is an expression, therefore the Standard implicitly forbids it. The Standard might have explicit statement forbidding this, but I think the above should be enough.

同样,您不能这样做:

namespace sample
{
  f(10,10); //error
  std::cout << "hello world" << std::endl;//error
}

但是如果您以某种方式将表达式转换为声明(或者更确切地说是在声明中使用表达式),那么您可以评估所谓的表达式.这是一个技巧:

But if you somewhow convert expressions into declarations (or rather use expressions in declarations), then you could evaluate the so-called expressions. Here is one trick:

#include<iostream>

namespace sample
{
  struct any { template<typename T> any(const T&){} };

  void f(int a,int b) { std::cout << a * b <<  std::endl; }

  any a1= (f(10,10), 0); //ok
  any a2 = std::cout << "hello world" << std::endl;//ok
}

int main() {}

输出(如果幸运的话):

Output (if you're lucky):

100
hello world

在线演示:http://ideone.com/icbhh

注意f()的返回类型是void,这意味着我不能写下面的(见错误):

Notice that the return type of f() is void, which means I cannot write the following (see error):

any a1 = f(10,10); //error

这就是我使用 comma 运算符的原因,以便表达式可以有一些值,该值计算为逗号表达式中的最后一个操作数.在std:cout的情况下,由于它返回std::ostream&,我不需要使用逗号运算符;没有它就好了.

That is why I used comma operator so that the expression could have some value, which evaluates to the last operand in the comma expression. In case of std:cout, since it returns std::ostream&, I don't need to use comma operator; it is fine without it.

上面代码中更有趣的一点是:为什么我在其中定义了 any 和一个 模板化 构造函数?答案是,我写这个是为了我可以分配任何类型的值(没有双关语),无论是intstd::ostream& 或其他什么.模板化构造函数可以接受任何类型的参数.

One more interesting thing in the above code: why I defined any and a templated constructor in it? The answer is, I wrote this so that I could assign value of any type (no pun intended), be it int, std::ostream& or whatever. The templated constructor can take argument of any type.

但是不要写这样的代码.不能保证它们按您期望的方式工作.

阅读本主题中的答案,您会明白为什么这种编码可能是危险的:

Read the answers in this topic where you would see why such coding could be dangerous:

  • main() 真的是 C++ 程序的开始吗?

这篇关于为什么语句不能出现在命名空间范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么语句不能出现在命名空间范围内?

基础教程推荐