这个 Cpp 示例代码什么意思,没看懂 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
James369
V2EX    Linux

这个 Cpp 示例代码什么意思,没看懂

  James369 2022-06-23 11:17:44 +08:00 3184 次点击
这是一个创建于 1238 天前的主题,其中的信息可能已经有所发展或是发生改变。

这段代码是用来计数小写字母的,但有个地方不理解

//Literal type that extends string literals: class conststr { const char* p; std::size_t sz; public: template<std::size_t N> constexpr conststr(const char(&a)[N]) : p(a), sz(N - 1) {} constexpr char operator[](std::size_t n) const { return n < sz ? p[n] : throw std::out_of_range(""); } constexpr std::size_t size() const { return sz; } }; constexpr std::size_t countlower(conststr s, std::size_t n = 0, std::size_t c = 0) { return n == s.size() ? c : s[n] >= 'a' && s[n] <= 'z' ? countlower(s, n + 1, c + 1) : countlower(s, n + 1, c); } // output function that requires a compile-time constant, for testing template<int n> struct constN { constN() { std::cout << n << '\n'; } }; int main() { std::cout << "the number of lowercase letters in \"Hello, world!\" is "; constN<countlower("Hello, world!")>(); // implicitly converted to conststr } 

不理解之处:构造的时候 constexpr conststr(const char(&a)[N]),这个 N 是怎么传进来的?

原文在此: https://en.cppreference.com/w/cpp/named_req/LiteralType, 怎么搞这么复杂的 template ,阅读起来有点障碍.

8 条回复    2022-06-23 16:16:20 +08:00
nmgwddj
    1
nmgwddj  
   2022-06-23 11:29:46 +08:00
主要是 constexpr 在作怪,参考: https://zh.m.wikipedia.org/zh-hans/Constexpr
cutepig
    2
cutepig  
   2022-06-23 11:45:35 +08:00 via Android
是 template 型推出的。如果用入,那麽就匹配到 ctor ,的模板被自推导出来
stein42
    3
stein42  
   2022-06-23 11:47:52 +08:00
字符串字面值 "Hello, world!" 的类型为 const char[14],所以推导出 N 为 14 。
GeruzoniAnsasu
    4
GeruzoniAnsasu  
   2022-06-23 11:50:38 +08:00
const char(&a)[N] 是一个 有 N 个元素的 char array 的引用
https://stackoverflow.com/questions/13081837/reference-to-an-array-in-c

并且 N 是一个模板参数,因此任意长的 string literal 都能匹配这个模板。N 是模板匹配的结果
Yienschwen
    5
Yienschwen  
   2022-06-23 12:27:43 +08:00   3
数组长度是数组类型的一部分,不同长度的数组,类型是不同的。所以长度是可以从类型中推断出来的。

顺带提一嘴,数组类型可以隐式转换到指针类型,但两个并不是一个东西。
https://coliru.stacked-crooked.com/a/71dad1817b07736c
realradiolover
    6
realradiolover  
   2022-06-23 14:50:59 +08:00
字符串常量嘛,编译时可以推导出来
echoechoin
    7
echoechoin  
   2022-06-23 15:42:16 +08:00
救命,cpp 太难了
James369
    8
James369  
OP
   2022-06-23 16:16:20 +08:00
@echoechoin 不会了,其实就是字符数组,我一时也没反应过来。
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1297 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 27ms UTC 17:17 PVG 01:17 LAX 09:17 JFK 12:17
Do have faith in what you're doing.
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86