gpt4 book ai didi

c++ - Clang 3.1 和用户定义的文字

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:20 26 4
gpt4 key购买 nike

Clang 3.1 声称支持用户定义的文字。我可以这样定义:

int operator"" _tryit(long double n) { return int(n); }

但是当我尝试使用它时出现错误:

int m = 5_tryit;

Invalid suffix '_tryit' on integer constant

最佳答案

5 在您的情况下不能隐式转换为 long double。您需要将其更改为 5.0 以使其成为 long double 或自己显式调用该函数以使隐式转换起作用:

int m = 5.0_tryit;

int n = operator"" _tryit(5);

(均使用 clang 版本 3.1(trunk)(llvm/trunk 155821) 进行了测试

This SO question对规则有很好的解释。

(另外,正如 abarnert 提到的,确保在编译时将 -std=c++11 标志传递给编译器)。

关于c++ - Clang 3.1 和用户定义的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889697/

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