显然,constexpr std::string尚未添加到GCC的libstdc++中(从 GCC v11.2 开始)。
这段代码:
#include <iostream>
#include <string>
int main()
{
constexpr std::string str { "Where is the constexpr std::string support?"};
std::cout << str << '\n';
}
Run Code Online (Sandbox Code Playgroud)
不编译:
#include <iostream>
#include <string>
int main()
{
constexpr std::string str { "Where is the constexpr std::string support?"};
std::cout << str << '\n';
}
Run Code Online (Sandbox Code Playgroud)
char当字符串包含超过 16秒(因为 GCC 的 SSO 缓冲区大小为 16)时,此类字符串将如何工作?简短的解释是什么?一个简单的构造函数会在堆栈上创建字符串对象并且从不使用动态分配吗?
这段代码:
std::cout << "is_trivially_constructible: "
<< std::boolalpha << std::is_trivially_constructible<const std::string>::value << '\n'; …Run Code Online (Sandbox Code Playgroud) 考虑最小化的代码片段:
#include <vector>
class Bar
{
public:
constexpr Bar() {}
};
consteval Bar foo()
{
return Bar();
}
int main()
{
std::vector<Bar> bars{ foo(), foo() };
}
Run Code Online (Sandbox Code Playgroud)
这不能在最新的 MSVC 编译器(Visual Studio 2022 版本 17.3.3)上编译,但可以在 Clang 或 GCC 上编译。
代码是否格式不正确,或者是 MSVC 中的错误?
我尝试运行类似的代码
#include <string>
#include <iostream>
int main() {
std::string str = "This is a string";
std::cout << str.starts_with("name");
}
Run Code Online (Sandbox Code Playgroud)
但是intellisense会报错
“std::__cxx11::basic_string<char, std::char_traits, std::allocator>”没有成员“starts_with” C/C++(135) [6,9]
它仍然可以构建并产生正确的结果。它还可以在头文件中找到实现。但是宏__cplusplus是定义的,因为201703L
我在构建时已经添加了命令-std=c++20,为什么会发生这种情况?
编译器:msys2编译的minGW 11.2
我是 FastAPI 的新手,在导入其他文件时遇到了这个问题。
\n我收到错误:\xe2\x80\x8d\xe2\x80\x8d\xe2\x80\x8d\xe2\x80\x8d
\nfrom . import schemas\nImportError: attempted relative import with no known parent package\nRun Code Online (Sandbox Code Playgroud)\n对于上下文,我从中导入的文件是一个名为 Blog 的文件夹。我看到某些 StackOverflow 答案说from . import schemas我应该写from Blog import schemas. 即使他们的解决方案是正确的,并且我在运行 python 程序时没有收到任何错误,当我尝试使用 uvicorn 运行 FastAPI 时,我收到此错误,并且我的本地主机页面未加载。
File "./main.py", line 2, in <module>\nfrom Blog import schemas\nModuleNotFoundError: No module named \'Blog\'\nRun Code Online (Sandbox Code Playgroud)\n\n主文件的代码如下所示:
\nfrom fastapi import FastAPI\nfrom Blog import schemas, models\nfrom database import engine\n\napp = FastAPI()\n\nmodels.Base.metadata.create_all(engine)\n\n\n@app.post(\'/blog\')\ndef create(request: schemas.Blog):\n return request\nRun Code Online (Sandbox Code Playgroud)\n模式.py
\n …我在 中有一个宏定义MyClass.h,如下所示:
#define _BufferSize_ 64
Run Code Online (Sandbox Code Playgroud)
我将 include 指令放置在MyClass.h以下内部main.cpp:
#include "MyClass.h"
Run Code Online (Sandbox Code Playgroud)
这是否意味着我可以_BufferSize_在main.cppand中使用MyClass.h?另外,这是好的做法吗?
我应该如何编写一个高效的异常类来显示可以通过在运行时修复源代码错误来防止的错误?
这就是我选择的原因std::invalid_argument。
我的异常类(显然不起作用):
class Foo_Exception : public std::invalid_argument
{
private:
std::string Exception_Msg;
public:
explicit Foo_Exception( const std::string& what_arg );
virtual const char* what( ) const throw( );
};
explicit Foo_Exception::Foo_Exception( const std::string& what_arg ) // how should I write this function???
{
Exception_Msg.reserve( 130000 );
Exception_Msg = "A string literal to be appended, ";
Exception_Msg += std::to_string( /* a constexpr int */ );
Exception_Msg += /* a char from a const unordered_set<char> */;
}
const char* Foo_Exception::what( ) …Run Code Online (Sandbox Code Playgroud) 我知道无符号整数是臭名昭著的,C++ 开发人员通常会避免使用无符号整数。我有一个包含两个int不应包含负值的成员变量的类:
.
.
.
private:
int m_Y_AxisLen;
int m_X_AxisLen;
.
.
.
Run Code Online (Sandbox Code Playgroud)
我以防止输入任何负数的方式设计了成员函数的逻辑。所以我确保这两个成员不会被分配负值。但是当我使用PVS-Studio
时,这也会带来一些警告。例如这里:
for ( int row = 0; row < getY_AxisLen( ); ++row )
{
for ( int column = 0; column < getX_AxisLen( ) - 1; ++column )
{
if ( m_characterMatrix[ row ][ column ] == getFillCharacter( ) )
{
m_characterMatrix[ row ][ column ] = fillCharacter;
}
}
}
Run Code Online (Sandbox Code Playgroud)
PVS-Studio 责怪我的索引row和类型column不正确memsize。这可能意味着我应该使用std::size_t row …
通过使用SonarLint分析代码,我收到了一条关于析构函数的消息(问题的标题),其声明如下:
class Foo
{
public:
. // default ctor
. // parameterized ctor
.
inline ~Foo() = default; // dtor
.
. // copy ctor = delete
. // copy assignment operator = delete
. // move ctor
. // move assignment operator
private:
...
mutable std::vector< std::vector<char> > m_Matrix;
...
};
Run Code Online (Sandbox Code Playgroud)
以下是消息的描述: 声明函数或静态成员变量 constexpr 使其隐式内联。
我不认为此类的 dtor 可以是constexprorconsteval因为它具有类型的非静态数据成员std::vector,因此~Foo必须delete[]在某个时刻调用以释放向量的存储。
那么为什么SonarLint显示此消息呢?是因为吗 = default?是否有任何 …
c++ destructor inline-functions sonarlint constexpr-function
哪种自旋锁方法更好(就效率而言)?
#include <atomic>
#define METHOD 1
int main( )
{
std::atomic_flag lock { };
#if METHOD == 1
while ( lock.test_and_set( std::memory_order_acquire ) )
{
while ( lock.test( std::memory_order_relaxed ) );
}
#else
while ( lock.test_and_set( std::memory_order_acquire ) );
#endif
lock.clear( std::memory_order_release );
}
Run Code Online (Sandbox Code Playgroud)
这个例子来自cppreference。test(std::memory_order_relaxed)当我们添加/删除对外部循环内部的调用时会发生什么?
我发现这两种方法(此处)生成的代码存在明显差异。
有没有办法将模板参数限制T为特定类型或类别?
下面的代码有效,但我想让它更简单:
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto func( const T num ) -> T
{
static_assert( std::is_floating_point_v<T>, "Floating point required." );
return num * 123;
}
int main( )
{
std::cout << func( 4345.9 ) << ' ' // should be ok
<< func( 3 ) << ' ' // should not compile
<< func( 55.0f ) << '\n'; // should be ok
}
Run Code Online (Sandbox Code Playgroud)
我想摆脱static_assert并写这样的东西:
template < std::is_floating_point_v<T> >
constexpr auto func( …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++20 ×5
stdstring ×2
arrays ×1
atomic ×1
c-strings ×1
consteval ×1
constexpr ×1
destructor ×1
exception ×1
fastapi ×1
header ×1
performance ×1
pvs-studio ×1
python ×1
python-3.x ×1
sonarlint ×1
spinlock ×1
swagger-ui ×1
templates ×1
visual-c++ ×1