小编jus*_*bie的帖子

为什么“int (*)(float)”指向“int foo()”会触发警告,而“int (*)(double)”则不会触发警告?

我有这段代码:

int foo() { return 0; }
int main()
{
    int (*float_function)(float) = foo;
}
Run Code Online (Sandbox Code Playgroud)

x86-64 GCC 12.2当使用, with编译时-Wall,它会产生警告(链接):

警告:从不兼容的指针类型“int (*)()”初始化“int (*)(float)”[-Win兼容指针类型]

但是,当我从 更改floatdouble链接)时:

int foo(){ return 0;}
int main()
{
    int (*double_function)(double) = foo;
}
Run Code Online (Sandbox Code Playgroud)

现在警告消失了。

但我认为这两者都应该受到警告。

我有什么地方说错了吗?为什么 GCC 不抱怨第二个例子?

c function-pointers language-lawyer

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

更改优化级别时 std::valarray 的求和结果会有所不同

我有这段代码求和std::valarray<int>

#include <iostream>
#include <valarray>
#include <vector>

int main()
{
    std::vector<std::valarray<int>> vectorOfValarrays{{1, 1}, {2, 2}, {3, 3}};
    std::valarray<int> sumOfValarrays(2);
    for (const auto& i : vectorOfValarrays)
      sumOfValarrays = sumOfValarrays + i;

    std::cout << sumOfValarrays[0] << ' ' << sumOfValarrays[1];
}
Run Code Online (Sandbox Code Playgroud)

x86-64 gcc 12.2使用-O0and进行编译-O1,它会打印预期结果:

6 6
Run Code Online (Sandbox Code Playgroud)

但是当使用-O2and编译时-O3,它会打印:

3 3
Run Code Online (Sandbox Code Playgroud)

这可能是什么原因?我的代码是未定义的行为还是这是一个 gcc bug?

c++ optimization valarray

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

我可以创建一个接受任意数量的相同类型参数的函数吗?

基本上,我想创建一个这样的函数:

total_sum(1, 2, 5, 4, 2) // return 14
total_sum(5, 6, 2) // return 13
Run Code Online (Sandbox Code Playgroud)

我可以使用的一种方法是省略号,如下所示:

#include <cstdarg>
int total_sum(int count, ...)
{
    int sum{0};
    va_list list;
    va_start(list, count);
    for (int arg{0}; arg < count; ++arg)
    {
         sum += va_arg(list, int);
    }
    va_end(list);
    return sum;
}
Run Code Online (Sandbox Code Playgroud)

但是当我打电话时total_sum(),我必须提供额外的参数。所以在这个例子中,我必须调用:

total_sum(5, 1, 2, 5, 4, 2);
total_sum(3, 5, 6, 2);
Run Code Online (Sandbox Code Playgroud)

我不太喜欢。另外,省略号确实很容易出错,所以我想远离它们。

我能想到的另一种方法是使用一些容器:

#include <vector>
int total_sum(std::vector<int> values)
{
    int sum{0};
    for(const auto &i : values)
    {
       sum += i;
    }
    return …
Run Code Online (Sandbox Code Playgroud)

c++

10
推荐指数
2
解决办法
1392
查看次数

常量表达式中指向静态 constexpr 变量的指针的比较

在以下代码中,比较static constexpr变量的指针x和是否不相等:y

int main() {
    static constexpr int x = 1;
    static constexpr int y = 2;
    static_assert( &x != &y );
}
Run Code Online (Sandbox Code Playgroud)

它在 Clang 中工作正常,但在打印的 GCC 中却不行

error: non-constant condition for static assertion
error: '((& x) != (& y))' is not a constant expression
Run Code Online (Sandbox Code Playgroud)

演示: https: //gcc.godbolt.org/z/3WdqP49Gq

这只是 GCC 中的一个错误吗?

c++ language-lawyer constexpr

5
推荐指数
0
解决办法
136
查看次数

为什么使用 std::find 会产生不同的结果(并且速度更慢)?在 std::array 上循环时,汇编代码比手写代码更好吗?

我有这段代码可以循环遍历 a std::array<int, 3>(请参阅Compiler Explorer)并查找元素是否在数组中。

#include <algorithm>
#include <iterator>
#include <array>
constexpr std::array<int, 3> arr = { 0, 1, 2};
bool forLoop(int inp) 
{
    for (int i {0}; i < arr.size(); ++i)
    {
        if (arr[i] == inp) 
        {
            return true;
        }
    }
    return false;
}
bool forEachLoop(int inp)
{
    for (int i : arr) 
    {
        if (i == inp) 
        {
            return true;
        }
    }
    return false;
}
bool STL(int inp)
{
    return std::find(arr.begin(), arr.end(), inp) != arr.end(); …
Run Code Online (Sandbox Code Playgroud)

c++ assembly stl compiler-optimization

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

C++ 无法为 lambda 中捕获的 Promise 调用 set_value ?

我正在尝试编写一个相当简单的方法来返回未来。lambda 决定了未来。这是一个最小的例子。实际上,lambda 可能会在不同的线程中调用,等等。

#include <future>

std::future<std::error_code> do_something() {
  std::promise<std::error_code> p;
  auto fut = p.get_future();
  auto lambda = [p = std::move(p)] {
    std::error_code error;
    p.set_value(error);
  };
  lambda();
  return std::move(fut);
}

int main() { return do_something().get().value(); }
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我收到类型错误。VSCode 智能感知 说:

没有重载函数“ std::promise<_Ty>::set_value [with _Ty=std::error_code]”的实例与参数列表和对象匹配(该对象具有阻止匹配的类型限定符) -- 参数类型为: ( std::remove_reference_t<std::error_code &>) -- 对象类型为:const std::remove_reference_t<std::promise<std::error_code> &>

MSVC 编译器说:

错误 C2663: ' std::promise<std::error_code>::set_value': 2 个重载没有对 ' this' 指针进行合法转换

我真的不明白 VS Code 错误。它是说它认为error是 aconst promise<error_code>吗?如何正确调用lambda 捕获中的set_value承诺? …

c++ boost-asio c++11 std-future

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

在 C++ 中,通过引用传递 const long double 是否合适?

我正在面对我的同事编写这样的函数定义:

    typedef long double Real;
    void func(const Real& x, const Real &y);
Run Code Online (Sandbox Code Playgroud)

我知道对原始类型使用 pass-by-ref 有点不好,但是呢long double?它的长度为80位,比现在普通机器上的64位指针要长。那么也许对于long double,pass-by-ref 比 pass-by-value 更有效?

c++

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

std::print() 线程安全吗?它是否存在文本交错问题?

std::print() 将在 C++23 中添加

我想知道是否std::print()是线程安全的,因为没有数据竞争

它是否存在文本交错问题,例如,如果我在线程 1 中存在:

std::print("The quick brown fox ")
std::print("jump over the lazy dog \n")
Run Code Online (Sandbox Code Playgroud)

和线程 2:

std::print("She sells ")
std::print("seashells by the seashore \n")
Run Code Online (Sandbox Code Playgroud)

它会以疯狂的顺序打印吗,就像这样:

She sells The quick brown fox seashells by the seashore \n
jump over the lazy dog \n
Run Code Online (Sandbox Code Playgroud)

我想这两个问题的答案都是肯定的,与 的行为相匹配std::cout,但是任何人都可以将我与标准所说的联系起来吗?

c++ multithreading language-lawyer fmt c++23

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

删除字符串中的零

这是我一直在努力的:

输入 2 个正整数(最多 100 位)。

比较两个整数。

注意:这 2 个整数可以包含前导零。(这意味着您必须先删除零)

因为 100 位太长,所以我使用string数据类型。

但是在我下面的程序中,它只是返回'=' 我调试它并发现for循环不起作用。

我的代码:

#include <iostream>
#include <string>

using namespace std; // I know this is bad but it's just a small program

char compareBigInterger(string str1,string str2)
{
    while (str1[0] != 0) str1.erase(0, 1);
    while (str2[0] != 0) str2.erase(0, 1);

    char answerHold {'='};
    if (str1.size() > str2.size())
    {
        answerHold = '>';
    }
    else if (str1.size() < str2.size())
    {
        answerHold = '<';
    }
    else …
Run Code Online (Sandbox Code Playgroud)

c++ string integer c++11

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