我在模板中有一些代码,如下所示:
if constexpr ( std::is_same_v<T, CompletedGeneration> ) {
auto stat = stats->getGenerationStats();
} else if constexpr ( std::is_same_v<T, CompletedReset> ) {
auto stat = stats->getResetStats();
} else if constexpr ( std::is_same_v<T, CompletedRun> ) {
auto stat = stats->getRunStats();
} else {
static_assert( false, "Invalid type for helper function" );
}
Run Code Online (Sandbox Code Playgroud)
在auto为stat只是为了得到它暂时编译。stats是类型T
在这个 if 语句之后,有一堆代码依赖于stat,所以显然我不能在if部分中定义它。我想知道,我将如何在 之外定义它if,因为它的类型取决于模板参数类型T(但不是它T本身)?
我是否必须指定一个额外的模板参数U,它接受类型为stat?或者必须使用某种继承?我宁愿避免这两种选择。
int main()
{
char MCU = 0b00000000;
char al_av = 0b10100000;
// Before bit operation
cout << "MCU = " << int(MCU) << endl;
MCU = MCU | al_av;
// After the bit operation
cout << "MCU = " << int(MCU) << endl; // Expected 160, got -96
char temp = 160;
cout << temp; // got the a with apostrophe
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我预计 的输出为char temp负数(或警告/错误),因为 160 超过了 [-127,127] 间隔,但结果是 ASCII 表中的那个(带有撇号的)
关于cpp 参考 …
我想知道 C++ 标准是否保证多维数组(不是动态分配的)被展平为完全相同空间的一维数组。例如,如果我有
char x[100];
char y[10][10];
Run Code Online (Sandbox Code Playgroud)
这两者是等价的吗?我知道大多数编译器都会扁平化y,但这真的能保证发生吗?阅读 C++ 标准的 11.3.4 数组部分,我实际上找不到任何保证这一点的地方。
C++ 标准保证y[i]紧跟在y[i-1]. 既然y[i-1]是10个字符长,那么,从逻辑上来说,y[i]在内存中应该发生后面10个字符;但是,编译器可以填充y[i-1]额外的字符以保持y[i]对齐吗?
使用 Moshi,您可以为类的字段自定义名称
@Json(name = "your name") string name
Run Code Online (Sandbox Code Playgroud)
但是您可以拥有多个自定义名称吗?
@Json(name = "your name" || "your/name" || "your-name") string name,
Run Code Online (Sandbox Code Playgroud)
那么要么 "your name" or"your/name"要么"your-name"会匹配?
对于下面的代码,我对 p3 向量感到困惑,为什么测试时没有触发移动运算符?对于p1和p2,我可以理解为所有元素都需要复制结束。
这是否意味着当我们移动向量时,它会修改向量对象,因此向量的元素将保持不变。而当我们复制向量时,我们必须复制每个元素。是否有向量运算会触发元素的移动运算符?
#include <iostream>
#include <vector>
using namespace std;
class Test {
public:
Test(int a) {
std::cout << " default " << std::endl;
}
Test(const Test& o) {
std::cout << " copy ctor " << std::endl;
}
Test& operator=(const Test& o) {
std::cout << " copy assign " << std::endl;
return *this;
}
Test(Test&& o) {
std::cout << " move ctor" << std::endl;
}
Test& operator=(Test&& o) {
std::cout << " move assign " << std::endl;
return *this; …Run Code Online (Sandbox Code Playgroud) 程序为什么以及如何给出输出?
#include <stdio.h>
int sum(int a ,int b , int c ){
return a , b ,c;
}
int main() {
int a=10, b=100 , c=1000 ;
int x = sum(a , b ,c);
printf("%d %d %d" ,x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:1000 1000 100
Variant我想用 C++创建某种类型。其实我想尽可能少地使用模板。这个想法是将值union与变量的类型一起存储,并根据存储的类型返回值。
所以测试代码如下所示:
#include <iostream>
#include <vector>
#include <cstring>
#include <typeinfo>
#include <typeindex>
using namespace std;
constexpr uint64_t mix(char m, uint64_t s)
{
return ((s << 7) + ~(s >> 3)) + static_cast<uint64_t>(~m);
}
constexpr uint64_t _(const char* str)
{
return (*str) ? mix(*str,_(str + 1)) : 0;
}
class Variant
{
public:
template<typename T>
Variant(T value):
m_info(typeid(value))
{
std::memcpy(&m_val, &value, sizeof(T));
}
auto toValue() ->decltype(???) // what have I use here ???
{
switch(_(m_info.name()))
{
case …Run Code Online (Sandbox Code Playgroud) 请参阅以下 g++ 程序。
#define seed1 0
#include <iostream>
#include <random>
int main()
{
double mean = 0.0;
double stddev = 1.0;
std::mt19937 generator1 (seed1);
std::normal_distribution<double> normal(mean, stddev);
std::cerr << "Normal: " << normal(generator1) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想获取生成器 1 的状态(作为种子)并删除生成器 1 以便稍后使用新种子再次实例化分布,并在我离开的地方继续我想将此代码放入函数中并调用它来生成高斯点在我想要的开始状态。并在函数结束时将状态保存为种子。
注意:MRE 示例太大,无法在此处发布(!),因此我将其存储在我的谷歌驱动器上,这里是一个链接 - https://drive.google.com/uc?id=1BUDD2DhumdY2eVwO5BffyaQfjVIdABb3&export=download
我需要调整使用 Xerces(1.6)/Xalan(1.3) 的非常旧的库以使用新的 Xerces(3.2.5)/Xalan(1.12)。原因很简单,因为使用现代 Microsoft C++ 编译器和 STL 几乎不可能构建此代码。
为了保持接口兼容,我必须对一些 Xerces/Xalan 对象使用包装类。包装器将 Xerces/Xalan 对象存储为void*指针。我想使用 RTTI 添加类型转换验证。但出了点问题...
这是强制转换验证函数的代码:
template<class T>
T* castAndCheck(const XmlLibObjectWrapper* pObjWrap)
{
T* result = (T*)(pObjWrap->EngineObj);
#ifdef _CPPRTTI
const type_info& ti = typeid(*result);
const type_info& tiExpected = typeid(T);
if (ti != tiExpected)
{
// I want it throw bad_cast here BUT IT DOES NOT!...
//T& resultSpecific = dynamic_cast<T&>(*result); //<- this has the same problem
T* resultSpecific = dynamic_cast<T*>(result);
if (resultSpecific == …Run Code Online (Sandbox Code Playgroud)