在一个相关的问题(“ std::string 格式,如 sprintf ”)中,我了解了这个很棒的新 C++20 标头<format>。
但是,似乎没有支持 compiler。这是正确的还是有办法使用它?
我正在使用带有-std=c++2a
标志的g++ 9.3,但<format>
无法识别库。
#include <format> // fatal error: format: No such file or directory
#include <iostream>
int main(){
std::cout << std::format("Hello {}!", "World");
}
Run Code Online (Sandbox Code Playgroud)
g++-9 test.cpp -o test -std=c++2a
我试图做github std::simd上给出的例子,但我的矢量化版本最终慢了 2-3 倍。如何正确使用?
该纪录片缺乏严肃的文档和进一步的示例用法。没有列出构造函数等。我确定我可能以错误的方式使用它,但是由于文档有限,我不知道如何继续。
g++ -o test test.cpp --std=c++2a -O0
#include <array>
#include <chrono>
#include <cstdlib>
#include <experimental/simd>
#include <iostream>
#include <random>
using std::experimental::native_simd;
using Vec3D_v = std::array<native_simd<float>, 3>;
native_simd<float> scalar_product(const Vec3D_v& a, const Vec3D_v& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
using Vec3D = std::array<float, 3>;
float scalar_product(const std::array<float, 3>& a, const std::array<float, 3>& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] …
Run Code Online (Sandbox Code Playgroud) 在我的矩阵类中,我按如下方式分配对齐内存:
/*** main.cpp ***/
#include "matrix.hpp"
int main()
{
{
Matrix(15, 17);
}
return 0;
}
/** matrix.hpp **/
class Matrix
{
private:
std::size_t width_, height_;
double* data_;
public:
Matrix(std::size_t width, std::size_t height);
~Matrix();
};
/** matrix.cpp **/
Matrix::Matrix(std::size_t width, std::size_t height)
: width_(width)
, height_(height)
{
data_ = new(std::align_val_t{64}) double[width_ * height_];
}
Run Code Online (Sandbox Code Playgroud)
我该如何正确删除它?
我都尝试过
Matrix::~Matrix()
{
delete[] data_;
}
Run Code Online (Sandbox Code Playgroud)
和
Matrix::~Matrix()
{
delete[](std::align_val_t{64}, data_);
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
SIGTRAP (Trace/breakpoint trap)
ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed 0x00007ffe5dc390e3
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc41512
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc4181a
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc4a7d9
ntdll!RtlGetCurrentServiceSessionId …
Run Code Online (Sandbox Code Playgroud) c++ memory-management memory-alignment new-operator delete-operator
#include <iostream>
#include <utility>
template<std::size_t... items>
constexpr std::size_t count()
{
return std::index_sequence<items...>().size();
}
template<std::size_t... items>
constexpr std::size_t fold_mul()
{
if( count<items...>() == 0 )
{
return 1;
}
else
{
return (... * items);
}
}
int main()
{
std::cout << "Result: " << fold_mul<>() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
此代码预计会输出,1
但会引发错误:
<source>:19:28: 错误:运算符 *
19 上的空扩展折叠| 退换货品);
我的问题是:为什么这不起作用,因为 fold_expression 显然在else
零件中。
作为参考,此实现有效:
template<typename... Args>
constexpr std::size_t fold_mul();
template<std::size_t... j>
requires (count<j...>() > 0)
constexpr std::size_t fold_mul()
{
return …
Run Code Online (Sandbox Code Playgroud) 已经有很多关于这个主题的帖子了,但是提议的解决方案都没有帮助我编译和/或链接我的代码。
一般建议的解决方案是前向声明类,前向声明运算符/函数,将其声明为友元,然后实现它。
我尝试编译的代码是:
#include "matrix.hpp"
int main()
{
using namespace MLL;
Matrix<int, 4, 4> a({1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16});
a+a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <algorithm>
#include <array>
#include <type_traits>
#include <vector>
namespace MLL{
template<typename data_t, std::size_t n_rows, std::size_t n_cols, std::size_t MAX = 256>
class Matrix;
template<typename data_t, typename T, std::size_t n_rows, std::size_t n_cols, std::size_t MAX, std::size_t other_MAX>
Matrix<decltype(std::declval<data_t>() + std::declval<T>()), n_rows, n_cols, std::min(MAX, other_MAX)>
operator+(Matrix<data_t, n_rows, n_cols, MAX> const& lhs, Matrix<T, n_rows, n_cols, other_MAX> const& rhs);
template<typename data_t, std::size_t n_rows, std::size_t n_cols, std::size_t …
Run Code Online (Sandbox Code Playgroud) 我有一个复杂结构的向量(此处std::pair<int, int>
)。我现在想将一个成员(比如说std::pair::first
)复制到一个新的向量中。有没有比这更好(更惯用)的方法
std::vector<std::pair<int, int>> list = {{1,2},{2,4},{3,6}};
std::vector<int> x_values;
x_values.reserve(list.size());
for( auto const& elem : list )
{
x_values.emplace_back(elem.first);
}
Run Code Online (Sandbox Code Playgroud) c++ ×6
c++20 ×2
algorithm ×1
copy ×1
fold ×1
formatting ×1
g++ ×1
header-files ×1
new-operator ×1
std ×1
stdvector ×1
templates ×1