You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

《C++编程原理与实践(第3版)》expect函数不存在问题

《C++编程原理与实践(第3版)》中expect函数未定义问题

我有Java和基础C学习基础,目前在用Bjarne的《C编程原理与实践(第3版)》学习C++,学到第4章错误处理部分时,发现书中提到的expect函数在编译时无法识别。

示例代码

// try to find a pair of values such that the precondition holds but the postcondition doesn't

import std;
using namespace std;

// calculate area of the rectangle
int area(int length, int width) {
    // if the arguments are positive
    expect([&] {return 0 < length && 0 < width; }, "bad arguments for area()"); // line of error
    int result = length * width;
    expect([&] {return 0 < result; }, "bad area() result"); // line of error
    return result;
}


int main() {
    // simple test, infinite loop
    while (true) 
        try {
            int length = 0, width = 0;
            cout << "Enter a length and a width: "; cin >> length >> width;
            cout << "Area is: " << area(length, width) << endl;
        } catch (...) {
            cerr << "An error occured" << endl;
        }

    return 0;
}

编译错误

使用Visual Studio 2022的c++latest编译选项时,出现以下错误:

trythis04.cpp
Z:\...\trythis04.cpp(9): error C3861: 'expect': identifier not found
Z:\...\trythis04.cpp(11): error C3861: 'expect': identifier not found

疑问与求助

我在cppreference上只找到了语法完全不同的expected,书中说明expect接收测试函数和错误提示字符串。想请教:

  • 这个问题是教材失误、我的操作有误还是标准后续修订导致的?
  • 应该用什么函数或方式替代书中的expect?

内容的提问来源于stack exchange,提问作者Xbox 360

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 12:49:59