我想根据类型将 a 移动或分配std::vector<Scalar>给a 。std::vector<float>Scalar
如果Scalar是float则移动,否则复制。
我试过这段代码
\n#include <vector>\n\nusing Scalar = double;\n\nint main()\n{\n std::vector<Scalar> x(10);\n std::vector<float> y(10);\n\n if constexpr(std::is_same<Scalar, float>::value)\n y = std::move(x); // line 11 (does not compile)\n else\n y.assign(x.begin(), x.end());\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n但我得到
\nmain.cpp:11:24: error: no match for \xe2\x80\x98operator=\xe2\x80\x99 (operand types are \xe2\x80\x98std::vector<float>\xe2\x80\x99 and \xe2\x80\x98std::remove_reference<std::vector<double>&>::type\xe2\x80\x99 {aka \xe2\x80\x98std::vector<double>\xe2\x80\x99})\n 11 | y = std::move(x);\n | ^\nRun Code Online (Sandbox Code Playgroud)\n我认为由于在编译时std::is_same<Scalar, float>::value评估为,那么下面的行将不会被编译。false
我使用编译它g++ -std=c++17 main.cpp -o …
我试图解析data.txt包含以下形式的十六进制的文件-0x1.0c7474fp+8:
-0x1.e51b67p+0 0x1.0b18ef5p+8 0x1.9c6075p+8 -0x1.190d44p+4
-0x1.e4f7838p+0 0x1.0b6446dp+8 0x1.9d779b8p+8 -0x1.16436ap+3
-0x1.e4555bp+0 0x1.0b6d446p+8 0x1.9e28c4ap+8 -0x1.1f8b0ep+3
-0x1.e3b9598p+0 0x1.0b7982cp+8 0x1.9edf5f8p+8 -0x1.f80bc6p+2
-0x1.e2f5896p+0 0x1.0b70bd7p+8 0x1.9f75c6ap+8 -0x1.07390ep+3
-0x1.e21d2dep+0 0x1.0b5961cp+8 0x1.9fef6e6p+8 -0x1.031012p+3
-0x1.e225b96p+0 0x1.0bbc104p+8 0x1.a12c75cp+8 -0x1.06951cp+3
-0x1.e17ec4cp+0 0x1.0bc35b7p+8 0x1.a1de5a6p+8 -0x1.25f138p+3
-0x1.e0e7f36p+0 0x1.0c140edp+8 0x1.a300c22p+8 -0x1.417644p+3
-0x1.e0e7e0ap+0 0x1.0c7474fp+8 0x1.a43f084p+8 -0x1.359f22p+3
-0x1.e076e6cp+0 0x1.0c93aebp+8 0x1.a50efaap+8 -0x1.00e406p+4
-0x1.e1c339cp+0 0x1.0ec7d62p+8 0x1.ab2777p+8 -0x1.1b6134p+3
-0x1.e0614eap+0 0x1.0e669d1p+8 0x1.ab301b8p+8 -0x1.6a997cp+2
-0x1.e01577p+0 0x1.0e9f2e5p+8 0x1.ac33c36p+8 -0x1.62eb42p+2
-0x1.de237dcp+0 0x1.0df5506p+8 0x1.abd2c42p+8 -0x1.8951c2p+2
Run Code Online (Sandbox Code Playgroud)
我写这段c++代码没有任何成功:
#include <iostream>
#include <fstream>
int main()
{
float a, b, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下命令来编译包含使用gcc版本6.3.0的这段c ++ 17代码:std::sampleg++ -std=gnu++17 -c main.cpp。
但是我明白了:error: ‘sample’ is not a member of ‘std’...
#include <vector>
#include <algorithm>
#include <random>
int main()
{
std::vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> b(5);
std::sample(a.begin(), a.end(),
b.begin(), b.size(),
std::mt19937{std::random_device{}()});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc 6是否支持使用 std::sample?(使用gcc 8.2.0可以正常编译)
我在这两页上找不到答案:
如何引用作为模板参数给出的类的别名模板到从模板基类继承的A类?CB
#include <vector>
struct A
{
// the alias template I want to refer to:
template<class T>
using Container = std::vector<T>;
};
// the base class
template<template<class> class _Container>
struct B
{
_Container<int> m_container;
};
template<class _A>
struct C : public B< typename _A::Container >
{// ^^^^^^^^^^^^^^^^^^^^^^
};
int main()
{
C<A> foo;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种解决方案,通过template在语句中的每个可能的位置添加关键字(例如template<class T> typename _A::Container<T>,typename _A::template Container...),但g++给出“模板参数 1 无效”或“类型/值不匹配”!