我编写了一个实用程序类(我猜它充当辅助类),它只有几个要在另一个类中使用的静态成员函数。它没有任何非静态成员(变量或函数)。所以它也没有任何显式的 ctors或dtor。
问题是,我的编译器(GCC v11.2 和 -std=c++20)是否仍然为实用程序类生成隐式默认 ctor 和 dtor ?如果是这样,那么我应该如何防止它这样做?因为我没有在代码中初始化该类的任何实例。
根据https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view,std::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) 一个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()成员函数向他们传递一个对象。这是不好的做法吗?我应该回到 …
我想声明 a std::array,但数组部分被识别为cli::array关键字(请参阅Why is "array" mark as a returned word in Visual-C++?),这意味着 thestd::不会影响它。如何自动停止Visual Studiousing namespace cli,或指定我要使用std::array?
蓝色数组字被识别为关键字
库中是否有标准算法可以完成以下 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) 问题很简单:在字符串缓冲区中查找空终止符( '\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) 作为第一个参数传递给它,会有什么不同吗?如果会,那么哪种政策适合这种特殊情况?
或者也许是我不知道的第三个选择?
下面两个 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) 例如,如果我想要一个默认的输出版本<<和一个详细的版本。
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)
这给出了错误“必须调用对非静态成员函数的引用”。
我想知道标准委员会是否已经修复了臭名昭著的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++ ×9
c++20 ×2
string-view ×2
c++-cli ×1
c++23 ×1
c-strings ×1
destructor ×1
fmt ×1
for-loop ×1
gcc-warning ×1
io ×1
lambda ×1
optimization ×1
ostream ×1
search ×1
stdarray ×1
stdstring ×1
string ×1