如何用C ++模板推导const char字符串的编译时间大小? [英] How to deduce the size of a compile time a const char string with C++ templates?
问题描述
I am trying to use clang++
to expand the template code from this post. Here is what I come up with.
constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) {
return i >= len ? throw i : i;
}
class StrWrap
{
unsigned size_;
char * const begin_;
public:
template< unsigned N >
constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) {
static_assert( N >= 1, "not a string literal");
}
constexpr char operator[]( unsigned i ) {
return requires_inRange(i, size_), begin_[i];
}
constexpr operator const char *() {
return begin_;
}
constexpr unsigned size() {
return size_;
}
};
constexpr unsigned count( StrWrap str, char c, unsigned i = 0, unsigned ans = 0 )
{
return i == str.size() ? ans :
str[i] == c ? count(str, c, i + 1, ans + 1) :
count(str, c, i + 1, ans);
}
int main(int argc, char const *argv[])
{
static_assert( count("dude", 'd') == 2, "d != 2" );
return 0;
}
但是,博客文章中的代码未编译:
However, the code from the blog post is not compiling:
<代码>/usr/bin/clang ++ -Xclang -ast-print -fsyntax-only test_debugger2.cpp>main.exe
/usr/bin/clang++ -Xclang -ast-print -fsyntax-only test_debugger2.cpp > main.exe
test_debugger2.cpp:12:48: error: cannot initialize a member subobject of type 'char *const' with an lvalue of type 'const char [5]'
constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) {
^ ~~~
test_debugger2.cpp:38:26: note: in instantiation of function template specialization 'StrWrap::StrWrap<5>' requested here
static_assert( count("dude", 'd') == 2, "d != 2" );
^
test_debugger2.cpp:38:20: error: static_assert expression is not an integral constant expression
static_assert( count("dude", 'd') == 2, "d != 2" );
^~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
I could find this related question Cannot initialize a member subobject of type 'const signed char *' with an lvalue of type 'const char [X]', but I did not get how I could fix it.
通过使用标志 -Xclang -ast-print -fsyntax-only
,我只要求 clang
扩展模板并将其扩展打印到标准输出:使用可变参数模板的编译时字符串"操作
By using the flags -Xclang -ast-print -fsyntax-only
I am only asking clang
to expand the templates and print their expansion to stdout: Compile-time 'String' Manipulation with Variadic Templates
我正在尝试学习使用C ++模板在编译时进行字符串计算,例如,字符串拆分,字符串连接,查找,替换,剪切等,该模板据说是图灵完成的:C++模板图灵完成?,然后,我以该博文中的示例为例,并使用 clang
查看它们如何工作以及如何使用它们来解决带有模板的任何编译时(可能)的计算.
I am trying to learn to do string computations, i.e., string splitting, string concatenation, find, replace, cut, etc, at compile time with C++ Templates which are said to be Turing Complete: C++ templates Turing-complete?, then, I am taking examples as that blog post and expanding them with clang
to see how they work and how I could use them to solve any compile time (possible) computations with templates.
这是我的编译器版本:
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin
如何修改该博客文章中的模板扩展?
How could I fix the template expansion from that blog post?
推荐答案
此
char * const begin_;
是指向 char
的 const
指针(指针是const,而不是它指向的东西).这个
is a const
pointer to char
(the pointer is const, not the thing it points to). This
const char(&arr)[N]
是对类型为 const char
的 N
个元素的数组的引用.数组的元素是const.您不能将指向非const的指针指向const元素数组(至少不是没有蛮力).您需要将指针设为 const char *
或使数组成为非常量元素的数组……
is a reference to an array of N
elements of type const char
. The element of the array are const. You cannot have a pointer to non-const point into an array of const elements (at least not without brute force). You need to either make your pointer a const char*
or make your array an array of non-const elements…
我想你是要写
const char* begin_;
注意:从C ++ 17开始,您可以仅使用 std:: string_view
…
Note: starting with C++17, you can just use std::string_view
…
这篇关于如何用C ++模板推导const char字符串的编译时间大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!