码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 表达式

时间:2018-03-21 13:57:02      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:输出   call   amp   clu   还需   .com   bsp   color   style   

参考:http://zh.cppreference.com/w/cpp/language/expressions

不求值表达式:表达式在编译期被使用,运行期无计算。例如:

#include <iostream>

int foo() noexcept {
    std::cout << "foo called ";
    return 0;
}

int main() 
{
    std::cout << typeid(foo()).name() << std::endl;
    std::cout << sizeof(foo()) << std::endl;
    std::cout << noexcept(foo()) << std::endl;
    std::cout << decltype(foo()){123} << std::endl;
}

但是,typeid有个例:令 typeid(expr), 当expr是广义左值(glvalue)时,expr是对应的运行期计算的(runtime evaluation )。例如:

class Base {
public:
    ~Base() {}
    virtual void foo() {}
};

class Derived : public Base{
public:
    virtual void foo() {}
};

Derived d;

Base& test() {
    std::cout << "test() called \n";
    return d;
}

int main()
{
    std::cout << typeid(test()).name();
    return 0;
}

执行此程序时,会看到cout输出:test() called.  如此行为的动机未明,还需要学习和查阅。

 

C++ 表达式

标签:输出   call   amp   clu   还需   .com   bsp   color   style   

原文地址:https://www.cnblogs.com/thomas76/p/8616091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!