小编Seb*_*ian的帖子

如何在添加自定义构造函数的同时保持聚合初始化?

如果我没有在结构中定义构造函数,我可以通过选择某个值来初始化它,如下所示:

struct Foo {
    int x, y;
};

Foo foo = {.y = 1};
Run Code Online (Sandbox Code Playgroud)

但是如果我添加新的默认构造函数,那么我就会失去这个功能:

struct Bar {
    int x, y;
    Bar(int value) : x(value), y(value) {}
};

Bar bar1 = 1;
Bar bar2 = {.y = 2}; // error: a designator cannot be used with a non-aggregate type "Bar"
Run Code Online (Sandbox Code Playgroud)

是不是两种方法都可以呢?

我尝试添加默认构造函数Bar () {},但似乎也不起作用。

c++ constructor aggregate-initialization

32
推荐指数
2
解决办法
3510
查看次数

使用 using 定义命名空间中的类

[class.pre] p3状态:

如果类头名称包含嵌套名称说明符,则该类说明符不应位于类范围内。 如果其类名是标识符,则类说明符应对应于嵌套名称说明符所引用的类、类模板或命名空间中可命名的一个或多个声明;它们都应具有相同的目标范围,并且类说明符的目标范围的目标范围就是该范围。

[例子:

namespace N {
  template<class>
  struct A {
    struct B;
  };
}
using N::A;
template<class T> struct A<T>::B {};    // OK
template<> struct A<void> {};           // OK
Run Code Online (Sandbox Code Playgroud)

--结束示例]

第一句话和例子似乎有所不同。

包含嵌套名称说明符(但仅“依赖”使用A<void>)的模板的专业化是否仍然符合?

clang 和 msvc 接受它,gcc 显示错误

错误:在其命名空间之外显式特化“模板结构 N::A”必须使用嵌套名称说明符 [-fpermissive]

附加背景

介绍原始措辞和示例注释的论文是P1787R6:声明以及在哪里找到它们,包含以下行:

template<> struct A<void> {};         // error: A not nominable in …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++23

11
推荐指数
0
解决办法
486
查看次数

在 Raku 中求解指数方程

指数方程

我正在尝试像这样解决这个指数方程:

my ($l,$r);

for (1 .. 100) -> $x {
    $l = $x * e ** $x;
    $r = 5 * (e ** $x - 1);
    say $x if $l == $r;
    }
Run Code Online (Sandbox Code Playgroud)

但它不起作用。如何以简单而全面的方式解决它?

equation solver equation-solving raku

8
推荐指数
1
解决办法
137
查看次数

什么时候从函数的参数化中省略 const 是安全的?

很多时候,我编写 C++ 函数时,似乎const比其他人使用了更多的修饰符。

例如,我编写 Excel .xlsx 文件,并为此使用 LibXL 库,并且文档提到了这样的函数:

bool writeNum(int row, int col, double value, Format* format = 0)
Run Code Online (Sandbox Code Playgroud)

我碰巧继承了这个函数,我的签名如下:

bool XLAPIENTRY writeNum(const int row, const int col, const double value, IFormatT<wchar_t> * format = 0);
Run Code Online (Sandbox Code Playgroud)

请注意const int行和列的 s。

const问题:省略s安全吗int?(很明显,对于指针来说,忽略指针的 const 是危险的,但是做一些 an 的本地副本int并不是那么危险)。请注意,包含 writeNum 函数的原始标头int也没有提到 const-ing s。为什么这里const省略了修饰符?

c++ constants parameter-passing function-signature

3
推荐指数
1
解决办法
94
查看次数

为什么按位运算符(~)的序列会是这样?那坏了吗?

#include <stdio.h>
#include <stdlib.h>

int main() {
unsigned char a=100,b=50;

printf("%d & %d = %d\n",a,b,a&b);
printf("%d | %d = %d\n",a,b,a|b);
printf("%d ^ %d = %d\n",a,b,a^b);
printf(" ~%d = %d\n",a, ~a);       /*the out come of this  line would be this: ~100 = -101 */
printf(" %d >> 2= %d\n",a, a>>2);
printf(" %d << 2= %d\n",a, a<<2);
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)

/结果应该是 155,不是吗?/

c++ printf bitwise-operators integer-promotion

1
推荐指数
1
解决办法
139
查看次数