<tfoot id='5K76v'></tfoot>
      1. <small id='5K76v'></small><noframes id='5K76v'>

        <legend id='5K76v'><style id='5K76v'><dir id='5K76v'><q id='5K76v'></q></dir></style></legend>
          <bdo id='5K76v'></bdo><ul id='5K76v'></ul>

        <i id='5K76v'><tr id='5K76v'><dt id='5K76v'><q id='5K76v'><span id='5K76v'><b id='5K76v'><form id='5K76v'><ins id='5K76v'></ins><ul id='5K76v'></ul><sub id='5K76v'></sub></form><legend id='5K76v'></legend><bdo id='5K76v'><pre id='5K76v'><center id='5K76v'></center></pre></bdo></b><th id='5K76v'></th></span></q></dt></tr></i><div id='5K76v'><tfoot id='5K76v'></tfoot><dl id='5K76v'><fieldset id='5K76v'></fieldset></dl></div>

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

        Why statements cannot appear at namespace scope?(为什么语句不能出现在命名空间范围内?)
        • <tfoot id='Cp7Jp'></tfoot>

            <small id='Cp7Jp'></small><noframes id='Cp7Jp'>

              • <i id='Cp7Jp'><tr id='Cp7Jp'><dt id='Cp7Jp'><q id='Cp7Jp'><span id='Cp7Jp'><b id='Cp7Jp'><form id='Cp7Jp'><ins id='Cp7Jp'></ins><ul id='Cp7Jp'></ul><sub id='Cp7Jp'></sub></form><legend id='Cp7Jp'></legend><bdo id='Cp7Jp'><pre id='Cp7Jp'><center id='Cp7Jp'></center></pre></bdo></b><th id='Cp7Jp'></th></span></q></dt></tr></i><div id='Cp7Jp'><tfoot id='Cp7Jp'></tfoot><dl id='Cp7Jp'><fieldset id='Cp7Jp'></fieldset></dl></div>

                <legend id='Cp7Jp'><style id='Cp7Jp'><dir id='Cp7Jp'><q id='Cp7Jp'></q></dir></style></legend>
                  <tbody id='Cp7Jp'></tbody>
                  <bdo id='Cp7Jp'></bdo><ul id='Cp7Jp'></ul>
                  本文介绍了为什么语句不能出现在命名空间范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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++ 程序的开始吗?

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                      <tbody id='fTd6L'></tbody>
                    • <tfoot id='fTd6L'></tfoot>
                      <legend id='fTd6L'><style id='fTd6L'><dir id='fTd6L'><q id='fTd6L'></q></dir></style></legend>

                      <i id='fTd6L'><tr id='fTd6L'><dt id='fTd6L'><q id='fTd6L'><span id='fTd6L'><b id='fTd6L'><form id='fTd6L'><ins id='fTd6L'></ins><ul id='fTd6L'></ul><sub id='fTd6L'></sub></form><legend id='fTd6L'></legend><bdo id='fTd6L'><pre id='fTd6L'><center id='fTd6L'></center></pre></bdo></b><th id='fTd6L'></th></span></q></dt></tr></i><div id='fTd6L'><tfoot id='fTd6L'></tfoot><dl id='fTd6L'><fieldset id='fTd6L'></fieldset></dl></div>
                    • <small id='fTd6L'></small><noframes id='fTd6L'>

                          <bdo id='fTd6L'></bdo><ul id='fTd6L'></ul>