0

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

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

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

第二个是函数,如果参数是 const 对象或临时对象,则返回 true。(与上述条件相同)。

我试过了

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

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

但它不像预期的那样工作

任何人都可以帮助我吗?

4

1 回答 1

1

Can anyone help me with that?

Yes. Since this is obviously homework, I'll give you a clue instead of giving you the answer, because giving you the answer wouldn't teach you anything.

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 is false, and anything that is not 0 is true, is that right? Knowing this, you can return an arithmetic expression that will represent true or false.

于 2013-03-28T18:29:45.793 回答