小编dig*_*evo的帖子

如果类未初始化,C++ 编译器是否会生成 def ctor?

我编写了一个实用程序类(我猜它充当辅助类),它只有几个要在另一个类中使用的静态成员函数。它没有任何非静态成员(变量或函数)。所以它也没有任何显式的 ctorsdtor

问题是,我的编译器(GCC v11.2 和 -std=c++20)是否仍然为实用程序类生成隐式默认 ctor 和 dtor ?如果是这样,那么我应该如何防止它这样做?因为我没有在代码中初始化该类的任何实例。

c++ destructor default-constructor

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

如果调用函数的特定重载,如何引发编译时错误?

根据https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_viewstd::basic_string_view类有 7 个重载的 ctor。我只关心其中的两个,因为现在我不在我的代码中使用其余的。

这些是我关心的实例:

constexpr basic_string_view( const CharT* s, size_type count );
constexpr basic_string_view( const CharT* s );
Run Code Online (Sandbox Code Playgroud)

我需要防止代码调用第二个代码(因为它可能导致非空终止缓冲区的缓冲区溢出)。

我有类似下面的内容:

#include <iostream>
#include <sstream>
#include <string>
#include <array>

void func( const std::string_view str )
{
    if ( str.empty( ) )
    {
        return;
    }

    std::stringstream ss;

    if ( str.back( ) == '\0' )
    {
        ss << str.data( );
    }
    else
    {
        ss << std::string{ str };
    }
}

int main()
{
    std::array<char, 20> buffer { }; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor-overloading string-view

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

string_view 与 const char* 性能

一个std::string_view参数比const char*下面代码中的一个参数更好吗?

void func( const std::string_view str )
{
    std::istringstream iss( str.data( ) ); // str is passed to the ctor of istringstream


    std::size_t pos { };
    int num { std::stoi( str.data( ), &pos, 10 ) }; // and here it's passed to std::stoi
}

int main()
{
    std::array<char, 20> buffer { };
    std::cin.getline( buffer.data( ), 20 );
    func( buffer.data( ) );
}
Run Code Online (Sandbox Code Playgroud)

std::istringstreamctor 和都std::stoi需要 aconst std::string&作为参数。但我std::string_view使用其data()成员函数向他们传递一个对象。这是不好的做法吗?我应该回到 …

c++ c-strings stdstring micro-optimization string-view

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

由于 cli::array 关键字 Visual Studio,C++ 无法使用 std::array

我想声明 a std::array,但数组部分被识别为cli::array关键字(请参阅Why is "array" mark as a returned word in Visual-C++?),这意味着 thestd::不会影响它。如何自动停止Visual Studiousing namespace cli,或指定我要使用std::array

蓝色数组字被识别为关键字

蓝色数组字被识别为关键字

c++ c++-cli visual-studio stdarray

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

寻找 std 算法来替换简单的 for 循环

库中是否有标准算法可以完成以下 for 循环的工作?

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>


int main( )
{
    const char oldFillCharacter { '-' };
    std::vector<char> vec( 10, oldFillCharacter ); // construct with 10 chars
    // modify some of the elements
    vec[1] = 'e';
    vec[7] = 'x';
    vec[9] = '{';

    const char newFillCharacter { '#' };

    for ( auto& elem : vec ) // change the fill character of the container
    {
        if ( elem == oldFillCharacter )
        {
            elem = newFillCharacter;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop array-algorithms c++20

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

找到字符串末尾的最快方法

问题很简单:在字符串缓冲区中查找空终止符( '\0' )位置的最快方法是什么?

std::array<char, 100> str { "A sample string literal" };
Run Code Online (Sandbox Code Playgroud)

我想一种选择是使用std::strlen.
我能想到的另一个选择是std::find或者甚至是std::ranges::find

const auto posOfNull { std::find( str.cbegin( ), str.cend( ), '\0' ) };
Run Code Online (Sandbox Code Playgroud)

ExecutionPolicy现在,如果将(eg std::execution::par) 作为第一个参数传递给它,会有什么不同吗?如果会,那么哪种政策适合这种特殊情况?

或者也许是我不知道的第三个选择?

c++ string optimization search c++20

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

如何在 lambda 函数中访问全局变量?

下面两个 lambda 函数哪一个是正确的?

#include <cstdlib>


extern constinit int exit_code { };

int main( )
{
    auto lambda_1 { [ &exit_code ]( ) noexcept
                    {
                        exit_code = EXIT_FAILURE;
                    } };

    auto lambda_2 { [ ]( ) noexcept
                    {
                        exit_code = EXIT_FAILURE;
                    } };

    lambda_1( );
    lambda_2( );

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

海湾合作委员会警告&exit_code

#include <cstdlib>


extern constinit int exit_code { };

int main( )
{
    auto lambda_1 { [ &exit_code ]( ) noexcept
                    {
                        exit_code = EXIT_FAILURE;
                    } };

    auto lambda_2 …
Run Code Online (Sandbox Code Playgroud)

c++ lambda global-variables gcc-warning

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

我如何用两种方式输出 &lt;&lt; 的内容?

例如,如果我想要一个默认的输出版本<<和一个详细的版本。

myClass myObject(//Constructor parameters);

cout << myObject << "\n";
cout << myObject.detailed << "\n";
Run Code Online (Sandbox Code Playgroud)

我尝试在我的班级中制作一个修改器,但这似乎不起作用。

class myClass {
public:
   friend std::ostream& operator<<(std::ostream& output, const myClass& myObject);
   std::ostream& detailed(std::ostream& output);
}
Run Code Online (Sandbox Code Playgroud)

这给出了错误“必须调用对非静态成员函数的引用”。

c++ operator-overloading ostream

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

C++23 `print` 是否检查写入是否成功进入流?

我想知道标准委员会是否已经修复了臭名昭著的Hello, world! 漏洞。我主要谈论新<print>库(尚未在任何编译器中使用)。

{fmt}库(它启发了标准库)尚未修复此问题。显然,它在输出时不会抛出任何异常/dev/full(从 v9.1.0 开始)。因此,使用 CI/O 函数(例如std::fflush错误处理)仍然是一件事。

下面的程序注意到错误并返回失败代码(因此没有错误):

#include <exception>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>


int main()
{
    fmt::println( stdout, "Hello, world!" );
    if ( std::fflush( stdout ) != 0 || std::ferror( stdout ) != 0 ) [[unlikely]]
    {
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这在 C++23 中可能吗?

#include <print>
#include <exception>
#include <cstdio>
#include <cstdlib>


int main()
{
    try
    {
        std::println( stdout, "Hello, world!" );
    }
    catch ( const std::exception& …
Run Code Online (Sandbox Code Playgroud)

c++ io error-handling fmt c++23

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