下面,您将找到一个用于CRC32计算的constexpr字符串文字.
我不得不重新解释字符串文字字符char
来unsigned char
.因为reinterpret_cast
在constexpr函数中不可用,所以解决方法是手动的两个补码的小实用函数,但我对它有点失望.
处理这种操作是否存在更优雅的解决方案?
#include <iostream>
class Crc32Gen {
uint32_t m_[256] {};
static constexpr unsigned char reinterpret_cast_schar_to_uchar( char v ) {
return v>=0 ? v : ~(v-1);
}
public:
// algorithm from http://create.stephan-brumme.com/crc32/#sarwate
constexpr Crc32Gen() {
constexpr uint32_t polynomial = 0xEDB88320;
for (unsigned int i = 0; i <= 0xFF; i++) {
uint32_t crc = i;
for (unsigned int j = 0; j < 8; j++)
crc = (crc >> 1) ^ (-int(crc & …
Run Code Online (Sandbox Code Playgroud) 我想创建一个我可以用作模板参数的字符串文字.它将编译器抛入某种无限循环.有什么问题并修复?
template <char...> struct slit { };
template <typename ...A>
constexpr auto make_slit(char const* const s, A const ...args)
{
return *s ? make_slit(s + 1, *s, args...) : slit<args...>();
}
int main()
{
auto const tmp_(make_slit("slit"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
强制性错误(带clang++ -std=c++1y
):
t.cpp:4:16: fatal error: recursive template instantiation exceeded maximum depth of 256
constexpr auto make_slit(char const* const s, A const ...args)
^
t.cpp:6:15: note: in instantiation of function template specialization 'make_slit<char, char, char, char, char, char, char, …
Run Code Online (Sandbox Code Playgroud) ID3D12Device::SetStablePowerState
函数调用仅在系统中打开开发人员模式时可用.如果没有,它会触发设备删除.
是否有一个API来检测开发人员模式是否打开,到目前为止我没有在msdn上发现允许应用程序查询它的任何内容.
我正在使用csv
和serde
crates 来反序列化 csv 文件。问题是最后一个字段实际上是一个逗号分隔的列表。
field1,field2,field3
xx, xx, str1, ..., strN
xx, xx,
xx, xx, str1, ..., strM
Run Code Online (Sandbox Code Playgroud)
这就是它在 Rust 中的映射方式,.flexible(true)
在阅读器上阅读它:
#[derive(Debug, Deserialize)]
struct Row {
field1: isize,
field2: isize,
field3: Vec<String>,
}
Run Code Online (Sandbox Code Playgroud)
如果 CSV 具有,field3
标题行,则一切正常。但有些文件没有它,我找不到让 serde 仍然填充Vec
. 我所能做的就是#[serde(default)]
放空field3
。
这是一个 Rust游乐场,显示了问题:
field1,field2,field3
xx, xx, str1, ..., strN
xx, xx,
xx, xx, str1, ..., strM
Run Code Online (Sandbox Code Playgroud) clang ++给出以下警告(见下面的代码):
'constexpr'非静态成员函数在C++ 1y中不会隐式'const'; 添加'const'以避免行为发生变化
应该在哪里const
添加?const constexpr size_t getSize() {
再发一次警告:
返回类型的'const'类型限定符无效
码:
constexpr size_t getSize()
{
return sizeof(header);
}
Run Code Online (Sandbox Code Playgroud) 鉴于下面的代码,unique_ptr和Bar之间的区别是什么,bar ( { new int } );
但没有foo( { new int } );
#include <memory>
struct Bar {
Bar() = default;
Bar( int* m ) : m_( m ) {};
~Bar() { if ( m_ ) delete m_; }
explicit operator bool() const { return m_; }
private:
int* m_ {};
};
bool bar( Bar && a ) { return bool(a); }
bool foo( std::unique_ptr<int> && a) { return bool(a); }
int main() {
bar( { } ); …
Run Code Online (Sandbox Code Playgroud) c++ ×3
c++14 ×3
constexpr ×3
c++11 ×2
clang ×1
const ×1
crc32 ×1
csv ×1
directx-12 ×1
rust ×1
serde ×1
unique-ptr ×1
windows-10 ×1