小编Ben*_*uch的帖子

在C++ 17中更改了受保护构造函数的规则?

我有这个测试用例:

struct A{ protected: A(){} };
struct B: A{};
struct C: A{ C(){} };
struct D: A{ D() = default; };

int main(){
    (void)B{};
    (void)C{};
    (void)D{};
}
Run Code Online (Sandbox Code Playgroud)

gcc和clang都用C++ 11和C++ 14模式编译它.两者都在C++ 17模式下失败:

$ clang++ -std=c++17 main.cpp 
main.cpp:7:10: error: base class 'A' has protected default constructor
        (void)B{};
                ^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
                     ^
main.cpp:9:10: error: base class 'A' has protected default constructor
        (void)D{};
                ^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
                     ^
2 …
Run Code Online (Sandbox Code Playgroud)

c++ constructor protected language-lawyer c++17

60
推荐指数
2
解决办法
3501
查看次数

为什么deque的pop_front()和pop_back()不是noexcept?

有什么理由说std::dequepop_front(),并pop_back()没有noexcept在C++ 11或更高或者是只是忘记了吗?

c++ deque c++-standard-library noexcept c++11

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

C++23 中 unique_ptr 的 nullptr init 规则是否发生变化?

此代码在 C++20 模式下使用 VS 2022 中的 MSVC 进行编译。它在 C++23 模式下失败。( /std:c++latest)

#include <memory>

struct A;

struct B {
    B();
    ~B();

    std::unique_ptr<A> ptr{nullptr};
};

int main(){
    B b;
}
Run Code Online (Sandbox Code Playgroud)

当前版本的 GCC 和 clang 在 C++23 模式下接受此代码。

编译器资源管理器上的实时代码

C++23 模式下的 MSVC:

#include <memory>

struct A;

struct B {
    B();
    ~B();

    std::unique_ptr<A> ptr{nullptr};
};

int main(){
    B b;
}
Run Code Online (Sandbox Code Playgroud)

微软回答我(尚未公开),新行为是由于constexpr std::unique_ptrC++23 模式造成的,他们只是遵循这个标准。

这是正确的吗?GCC 和 clang 在 C++23 模式下接受此代码是错误的吗?


我将对这里发生的情况添加更多解释,因为一些评论表明这并不明显。

std::unique_ptr<A>这里调用了的构造函数。它不会调用不完整类型的构造函数A,因为它是用nullptr. 这部分对于 C++23 模式下的 …

c++ unique-ptr visual-c++ language-lawyer c++23

13
推荐指数
0
解决办法
421
查看次数

constexpr函数模板何时实例化?

我正在制定一个提出功能头功能的提议constexpr.(std::invoke,std::reference_wrapper,std::bind,std::mem_fn,std::not_fn)

我已经了解到添加constexpr可以破坏现有代码,因为constexpr函数是急切实例化的.

template<class T>
int f(T){
    return T::not_existing_member;
}

template<class T>
constexpr int g(T){
    return T::not_existing_member;
}

int main(){
    decltype(f(0)) a; // Well-formed
    decltype(g(0)) b; // Ill-formed if the function body is instantiated
}
Run Code Online (Sandbox Code Playgroud)

GCC编译这段代码,clang没有.我在我的提议中描述了如何使用示例来处理带有重载的急切实例化std::bind.

你能告诉我编译器必须在标准中描述的位置,以及何时允许实例化一个功能模板?

更确切地说,我想知道在以下示例中,GCC和clang的相同行为是由标准强制执行还是实现定义:

template<class T>
struct Foo{
    constexpr int f(){
        return 0;
    }

    constexpr int f()const{
        return T::not_existing_member;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates instantiation constexpr

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

C++26 中静态反射需要什么语法?

据我所知,静态反射目前在 C++26 的路线图上。

\n

反射TS提出了基于类型的语法,但同时也提出了基于值的语法。在P2560 Mat\xc3\xba\xc5\xa1 Chochl\xc4\xb1\xcc\x81k 中,对两种方法进行了比较。

\n

是否已经决定哪种方法可能标准化?

\n

c++ reflection static-reflection c++26

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

clang 标准库中的 __nat 类

我在翻看clang的C++标准库,在shared_ptr类中发现了这个类。

class shared_ptr
// ...
private:
    struct __nat {int __for_bool_;};
// ...
};
Run Code Online (Sandbox Code Playgroud)

据我所知,此类用于在编译时检测是否可以进行类型转换,但其成员__for_bool_从未在类或weak_ptr对应类的任何地方使用。所以,我的问题是,有什么意义__for_bool_?为什么不简单地使用一个空类来达到同样的目的呢?

我确信标准库的作者肯定比我更了解。

c++ std clang shared-ptr

8
推荐指数
0
解决办法
251
查看次数

WebSocket 是否与 HTTP/3 兼容

即将到来的 HTTP/3 标准不再基于 TCP,而是基于 QUIC 协议。WebSocket 基于 TCP 并作为 HTTP 更新启动。

是否基于 QUIC 而不是 TCP 作为 HTTP/3 更新启动的 WebSocket 连接?或者无法将 HTTP/3 连接更新为 WebSocket 连接?

http websocket http3

8
推荐指数
2
解决办法
2520
查看次数

Pivotal Cloud Foundry 与 VMware Tanzu 应用服务

Pivotal Cloud Foundry (PCF) 与 VMware Tanzu 应用服务:哪个更好?它们相同吗?还是只是改了名字?或者这只是一次升级,PCF 上的旧应用程序可以升级到 Tanzu 吗?

cloud vmware cloud-foundry tanzu-application-service

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

C++20“寻址限制”有什么用?

根据 cppreference.com,C++20 引入了标准库函数的“寻址限制” :

寻址限制

如果 C++ 程序显式或隐式尝试形成指针、引用(对于自由函数和静态成员函数)或指向成员的指针(对于非静态成员函数),则该程序的行为是未指定的(可能是格式错误的)标准库函数或标准库函数模板的实例,除非它被指定为可寻址函数(见下文)。

以下代码在 C++17 中定义良好,但会导致未指定的行为,并且自 C++20 起可能无法编译:

#include <cmath>
#include <memory>
 
int main()
{
    auto fptr0 = &std::betaf; // by unary operator&
    auto fptr1 = std::addressof(std::betal); // by std::addressof
    auto fptr2 = std::riemann_zetaf; // by function-to-pointer implicit conversion
    auto &fref = std::riemann_zetal; // forming a reference
}
Run Code Online (Sandbox Code Playgroud)

为什么要引入这个?

特别是在语言的向后兼容性方面,这似乎是一个破坏了许多现有代码的更改。这带来了什么好处,值得进行如此重大的改变?

c++ std c++20

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

C++23 中是否有支持 const/ref/noexcept 的可复制函数包装器?

C++23 引入了对隐式参数和函数本身的std::move_only_function支持。constl/r-value-referencethisnoexcept(true/false)

\n

旧的std::function缺乏这些超载。

\n
#include <functional>\n\nint main(){\n    std::function<void()>([]noexcept(true){});          // okay \xe2\x9c\x93\n    std::function<void()>([]noexcept(false){});         // okay \xe2\x9c\x93\n    std::function<void()noexcept>([]noexcept(true){});  // not supported\n    std::function<void()noexcept>([]noexcept(false){}); // not supported\n\n    std::move_only_function<void()>([]noexcept(true){});          // okay \xe2\x9c\x93\n    std::move_only_function<void()>([]noexcept(false){});         // okay \xe2\x9c\x93\n    std::move_only_function<void()noexcept>([]noexcept(true){});  // okay \xe2\x9c\x93\n    std::move_only_function<void()noexcept>([]noexcept(false){}); // fails \xe2\x9c\x93\n\n    std::function<void()>([i=0]{});             // okay \xe2\x9c\x93\n    std::function<void()>([i=0]mutable{});      // okay \xe2\x9c\x93\n    std::function<void()const>([i=0]{});        // not supported\n    std::function<void()const>([i=0]mutable{}); // not supported\n\n    std::move_only_function<void()>([i=0]{});             // okay \xe2\x9c\x93\n    std::move_only_function<void()>([i=0]mutable{});      // okay \xe2\x9c\x93\n    std::move_only_function<void()const>([i=0]{});        // okay \xe2\x9c\x93\n    std::move_only_function<void()const>([i=0]mutable{}); // fails \xe2\x9c\x93\n}\n …
Run Code Online (Sandbox Code Playgroud)

c++ c++23

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