小编Bar*_*rry的帖子

可以在 C++17 中编译,但不能在 C++20 中编译

我有一个非常简单的代码示例。我可以在 C++17 中编译它,但不能在 C++20 中编译它。为什么不?

struct S
{
    S(const S&) = delete;
    S( S&& ) = default;

    int k;
    int m;
    int n;
};

int main()
{
    S s1{10, 20, 30};
    return s1.m;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++20

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

在推导函数参数时是否需要考虑隐式用户定义转换运算符() 的解决方法?

考虑以下示例,它尝试将 a 传递std::array给函数。当然不考虑“转换”,但是有没有不需要明确的解决方法?特别是如果该类已经提供了必要的属性(value_type 等)。

template <typename T, size_t N>
struct Array_t
{
    using value_type = T;
    using size_type = size_t;

    std::array<T, N> Internal{};

    constexpr operator auto() { return Internal; }
};

template <typename T, size_t N> constexpr bool Test(std::array<T, N> Input)
{
    return Input.size() == 32;
}

constexpr std::array<uint8_t, 32> OK1 = Array_t<uint8_t, 32>();
constexpr auto OK2 = Test((std::array<uint8_t, 32>)Array_t<uint8_t, 32>{});
constexpr auto OK3 = Test(Array_t<uint8_t, 32>().Internal);

// could not deduce template argument for 'std::array<_Ty,_Size>'
// from 'Array_t<uint8_t,32>' …
Run Code Online (Sandbox Code Playgroud)

c++ templates types c++20

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

概念:需要类型上的函数,而不默认构造该类型

我需要要求某种类型 A 存在一个函数 f(A, A::B)。

我正在通过使用 A 和 A::B 的实例调用 f 来测试这一点。是否有一种不那么招摇的方法来测试依赖类型的实例而不需要默认可构造?

template <class Container>
concept CanPutData = requires (Container a)
{
  //put(typename Container::data_type{}, a);  // overconstrained
  put(*reinterpret_cast<typename Container::data_type*>(0), a); // oof
};

void test(CanPutData auto container) {}

template<class Container>
void put(typename Container::data_type const& data, Container& into) {}


template<class Data>
struct data_container { using data_type = Data; };
struct not_default_constructible_data { int& v; };

int main()
{
  test(data_container<not_default_constructible_data>{});

  return 0;
}

Run Code Online (Sandbox Code Playgroud)

c++ templates c++-concepts c++20

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

什么时候(*b)是假的?

我只是在玩指针,我有一个问题.

我写了以下while循环:

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int *b = &a;

    while(*b){ cout << "true"; break; }

    cout << endl;
    cout << b;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

条件是什么时候,(*b)是假的?如果我没有将它指向一个值,代码就不会编译.

另外,你如何将条件测试翻译成英语?

c++

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

c++ &lt;错误:无法访问地址 0x1 处的内存&gt;

我真的不明白为什么我的代码有这个问题

首先,我创建了两个指向 char 的指针

char* finWord;
char* ignoredWord;
Run Code Online (Sandbox Code Playgroud)

然后我将它们作为参数传递给其他函数

lowerCase(line.substr(0, endWord), ignoredWord);
toNormalWord(ignoredWord, finWord);
Run Code Online (Sandbox Code Playgroud)

但是当我运行程序时,它抛出了一个分段错误,问题是 finWord 地址总是 0x1

这里是问题发生的地方

void toNormalWord (string src, char* des) 
{
    char c;
    des[sizeof(src) + 1];
    int position = 0; 

    if (isThere)
    {
        des[position] = c; //Here the gdb show me the following error 0x1 <error: 
                           // Cannot access memory at address 0x1>
        position++;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么gets()需要更多的角色

这是我的代码

char c[3];
gets(c);
puts(c);
Run Code Online (Sandbox Code Playgroud)

这里char变量c有3个索引.但如果我输入超过3个字母,那么我的代码打印超过3个字母,我打字.但是怎么可能c一次只能存储3个字符.不是吗?

c c++

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

C++将结构中的所有字节加起来

给定一个这样的压缩结构:

struct RSDPDescriptor {
    char Signature[8];
    uint8_t Checksum;
    char OEMID[6];
    uint8_t Revision;
    uint32_t RsdtAddress;
} __attribute__ ((packed));
Run Code Online (Sandbox Code Playgroud)

如何将其中的所有单个字节相加?

c++ struct osdev

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

剪辑功能的标准库版本

我已经编写了一个名为的实用程序nostd::clip,可以x在a floor和a 之间剪切提供的值ceiling

namespace nostd {
    template<class T>
    auto clip(T floor, T x, T ceiling) -> T
    {
        return std::min(ceiling, std::max(floor, x));
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有一些功能std可以完成相同的工作,我可以用它代替?也许在C ++ 17中?

c++ c++17

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

我们应该采用新的自我分配保护方式吗?

所有

传统上,在有关C ++的书籍中,甚至在核心准则中,自我分配保护都写为

A& A::operator=(const A& a) {
    if (&a != this) {
       ...
    }
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

但是在现代C ++中(自C ++-11起),我们有了std :: addressof魔术。

如果我要教给学生现代C ++的所有好处,我是否应该提倡将自赋值检查写成:

A& A::operator=(const A& a) {
    if (std::addressof(a) != this) {
       ...
    }
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

更笼统的问题-《核心指南》和其他地方应该走这条路吗?

有什么想法吗?评论?

c++ standards c++11 c++17

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

'make_pair'不能用作函数

这是我的完整代码:

  #include <iostream>
#include <vector>
#include <bits/stdc++.h> 

using namespace std;
#define ff first
#define mp make_pair

#define ss second


int main(void) {

    int m;
    vector <string> grid;

    cin >> m;

    pair <int,int> foo;
 pair <int,int> bar;


// bar =make_pair (10.5,'A');
foo = make_pair (1,2);
cout<<foo.ss<<endl;
    for(int i=0; i<m; i++) {
        string s; cin >> s;
        grid.push_back(s);
        int pp = s.find('p');
        int mp = s.find('m');
        if(pp>=0){
          bar = make_pair(pp,i);
        }
      cout<<pp<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

prog.cpp: In function 'int main()': …
Run Code Online (Sandbox Code Playgroud)

c++ std-pair c++14

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

标签 统计

c++ ×10

c++20 ×3

c++17 ×2

templates ×2

c ×1

c++-concepts ×1

c++11 ×1

c++14 ×1

osdev ×1

standards ×1

std-pair ×1

struct ×1

types ×1