zoukankan      html  css  js  c++  java
  • C++的几个晦涩特性

    Obscure C++ Features - Made by Evan列举了几个C++的晦涩特性,挺有意思的,下面简单列几个:

    []是啥意思

    ptr[3]只是 *(ptr + 3) 的简写,所以也就是 *(3 + ptr),所以它和 3[ptr] 是一样的。

    运算符重载

    运算符重载可以做很多奇怪的事情,比如可以通过运算符重载实现python style的print。如下:

    namespace __hidden__ {
        struct print {
            bool space;
            print() : space(false) {}
            ~print() { std::cout << std::endl; }
    
            template <typename T>
            print &operator , (const T &t) {
                if (space) std::cout << ' ';
                else space = true;
                std::cout << t;
                return *this;
            }
        };
    }
    
    #define print __hidden__::print(),
    
    int main() {
        
         int a = 1, b = 2;
    
         print "this is a test";
         print "the sum of", a, "and", b, "is", a + b;
    
        return 0;
    }

    函数的try

    可以在函数名称后面加上try,对应整个函数体。例如下面的代码。

    int f() { throw 0; }
    
    // Here there is no way to catch the error thrown by f()
    struct A {
      int a;
      A::A() : a(f()) {}
    };
    
    // The value thrown from f() can be caught if a try-catch block is used as
    // the function body and the initializer list is moved after the try keyword
    struct B {
      int b;
      B::B() try : b(f()) {
      } catch(int e) {
      }
      int B::C() try{
          throw 2;
          return 1;
      } catch(int e) {
          cout<<"exception "<<e<<endl;
      }
    };
  • 相关阅读:
    容斥原理
    m元集A到n元集B的满射的个数
    二项式反演公式
    多项式定理
    组合数的基本性质
    Luogu P2408 不同子串个数
    Luogu P5410【模板】扩展 KMP
    Luogu P2336 [SCOI2012]喵星球上的点名
    Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns
    Luogu P4248 [AHOI2013]差异
  • 原文地址:https://www.cnblogs.com/fresky/p/3063235.html
Copyright © 2011-2022 走看看