English 中文(简体)
静态——如果测试条件包括同 con相定义的固定不变,就会失败吗?
原标题:static_assert fails when test condition includes constants defined with const?

I m宣读了Bjarne Stroustrup的书,“C++方案编制语言”,我发现了一个解释static_assert的例子。 我的理解是,static_assert只适用于能够用经常表述的方式表述的内容。 换言之,它绝不能包括意图在时间评估的表述。

The following example was used in the book (I does some change in the Code). 但我认为,这不应改变本书所列原始范例法典所产生的任何东西。

#include <iostream>

using namespace std;

void f (double speed)
{
    constexpr double C = 299792.468;
    const double local_max = 160.0/(60*60);
    static_assert(local_max<C,"can t go that fast");
}

int main()
{
        f(3.25);
    cout << "Reached here!";
    return 0;
}

以上是汇编错误。 http://ideone.com/C97oF5“rel=”http://ideone.com/C97oF5

The exact code from the book example:

constexpr double C = 299792.458;
void f(double speed)
{ 
    const double local_max = 160.0/(60∗60);
    static_assert(speed<C,"can t go that fast"); // yes this is error
    static_assert(local_max<C,"can t go that fast");
 } 
问题回答

由于该书的《刑法》是错误的,而且根本不正确,后来同一作者在《C++号航程》书第二版的一本不同的书上加以更正。

错误是:

const local_max = 160.0/(60*60);

could

static_assert(local_max<C,"can t go that fast");

because the const variables are evaluated at run-time while static_assert(...) requires compile time variables. constexpr , however, is evaluated during compile time and hence can be used in the static_assert(...) function. Hence the correct way to define local_max is by using the constexpr keyword and not const

constexpr local_max = 160.0/(60*60);

旧(校正)Snippet,见“C++方案编制语言”书。

constexpr double C = 299792.458; // km/s
void f(double speed) {
    const double local_max = 160.0/(60*60); // 160 km/h == 160.0/(60*60) km/s
    static_assert(speed<C,"can t go that fast"); // error: speed must be a constant
    static_assert(local_max<C,"can t go that fast"); // OK // ...
}

Updated (Correct) Snippet as found in "A Tour of C++ (second edition)"

constexpr double C = 299792.458; // km/s
    void f(double speed) {
        constexpr double local_max = 160.0/(60∗60); // 160 km/h == 160.0/(60*60) km/s
        static_assert(speed<C,"can t go that fast"); // error: speed must be a constant
        static_assert(local_max<C,"can t go that fast"); // OK // ...
    }

 

汇编者在汇编时间时不知道<代码>speed的价值。 据认为,它无法在汇编时间评价<代码>speed < C。 因此,在处理线时,预计会出现一个汇编的时间错误。

static_assert(speed<C,"can t go that fast");

语言并不能保证在汇编时间时评估浮动点表述。 一些汇编者可能支持这项工作,但这种支持并不依赖。

尽管浮动点变量的数值是对人类读者的“客户”,但不一定在汇编时间时加以评估。 汇编者从所提供的链接中发出的错误信息表明了这一点。

static_assert expression is not an integral constant expression

You ll have to find a way to do the comparison using integral expressions. However, that seems to be a moot point. I suspect, what you really want to do is make sure that speed is within a certain limit. That makes sense only as a run time check.





相关问题
Separating Business Layer Errors from API errors

The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

How to tell why a file deletion fails in Java?

File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...

热门标签