gpt4 book ai didi

C++模板检查值

转载 作者:行者123 更新时间:2023-11-28 07:41:53 25 4
gpt4 key购买 nike

我需要编写 2 个我不知道如何处理的函数模板。

第一个是仅当函数参数为 100(我假设 int 等于 100)时才返回 true 的函数,但结果应该在编译期间已知(没有 RTTI)。

编辑:我不知道语法。我认为应该这样做:http://blog.emptycrate.com/node/271

第二个是如果参数是常量对象或临时对象则返回 true 的函数。 (条件同上)。

我试过了

template<class T>
bool isConst(T a) {
return false;
}

template<class T>
bool const& isConst(T const& a) {
return true;
}

但它并没有像预期的那样工作

有人可以帮我吗?

最佳答案

Can anyone help me with that?

是的。由于这显然是家庭作业,所以我会给你一个线索而不是给你答案,因为给你答案不会教给你任何东西。

First one is a function that returns true only if the function parameter is 100 (I assume int that is equal to 100) but the result should be known during compilation (without RTTI).

0 为假,不为 0 的为真,对吗?了解这一点后,您就可以返回一个表示真或假的算术表达式

关于C++模板检查值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15689366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com