码:
std::vector<int> x{1,2,3,4};
std::array<int, 4> y{{1,2,3,4}};
Run Code Online (Sandbox Code Playgroud)
为什么我需要std :: array的双花括号?
我试图了解std :: ref的工作原理.
#include <functional>
#include <iostream>
template <class C>
void func(C c){
c += 1;
}
int main(){
int x{3};
std::cout << x << std::endl;
func(x);
std::cout << x << std::endl;
func(std::ref(x));
std::cout << x << std::endl;
}
Output : 3 3 4
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我认为C第三个函数调用的模板参数被实例化为std::reference_wrapper<int>.在阅读参考文献时,我注意到没有+=操作员std::reference_wrapper<int>.那么,如何c += 1;有效?
我想创建一个返回整数幂的函数.请阅读fmuecke 在c ++中的整数幂解决方案 .
但是,我想将他的解决方案推广到任意类型T.由于c ++ 11有constexpr,我想这是可能的.
天真,我尝试过类似的东西,
template<class T, int N>
inline constexpr T pow(const T x){
return pow<N-1>(x) * x;
}
template<class T>
inline constexpr T pow<T, 1>(const T x){
return x;
}
template<class T>
inline constexpr T pow<T, 0>(const T x){
return 1;
}
Run Code Online (Sandbox Code Playgroud)
实际上这种方法失败了,因为不允许对函数模板进行部分特化.
还有一个问题.我听说编译器是否在编译时评估constexpr函数是由编译器决定的.如何强制它计算一般类型.我从某处读到,对于整数consts来说,最简单的一个方法是将它包装在std :: integral_const :: value中.
我在代码下面编译错误.
struct B{
double operator()(){
return 1.0;
}
};
struct A {
auto func() -> decltype(b())
{
return b();
}
B b;
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我重组A,它会编译.
gcc 4.8表示'b'未在此范围内声明.
struct A {
B b;
auto func() -> decltype(b())
{
return b();
}
};
Run Code Online (Sandbox Code Playgroud)
那么,第一个出了什么问题?
int main(){
int x{};
auto x2 = x;
auto x3{x};
static_assert(is_same<int, decltype(x)>::value, "decltype(x) is the same as int");
static_assert(is_same<int, decltype(x2)>::value, "decltype(x2) is the same as int");
static_assert(is_same<int, decltype(x3)>::value, "decltype(x3) is the same as int"); // Error here.
}
Run Code Online (Sandbox Code Playgroud)
此代码无法使用gcc 4.8.0进行编译.我甚至没猜到它的类型decltype(x3).它是什么?为什么行为不同?
对于过于含糊的标题感到抱歉.(由于缺乏我的英语技能).请建议一个更好的标题.
请考虑以下代码.
struct A {
typedef std::vector<double> State;
// template <class... Args>
// A(Args... args)
// : a(args...)
// {}
template <class... Args>
A(Args&&... args)
: a(std::forward<Args>(args)...)
{}
A(const A&) = default;
A(A&&) = default;
State a;
};
int main(){
A a(3,2);
A b = a; // This line triggers an error!!
}
Run Code Online (Sandbox Code Playgroud)
Gcc 4.8.0无法使用错误消息进行编译
error: no matching function for call to 'std::vector<double>::vector(A&)' : a(std::forward<Args>(args)...).
我无法理解为什么这段代码错了.在我看来,编译器应该在行中调用复制构造函数A b = a;.
但是,如果我用注释的(它只是取值)替换构造函数.它确实编译.此外,现在不需要默认复制(和移动)构造函数的行.这里发生了什么?
这是一个简单的问题.代码优先.
struct A {
int x;
};
struct B {
bool y;
};
struct C {
int x;
bool y;
};
Run Code Online (Sandbox Code Playgroud)
在主要功能中,我打电话
cout << " bool : " << sizeof(bool) <<
"\n int : " << sizeof(int) <<
"\n class A : " << sizeof(A) <<
"\n class B : " << sizeof(B) <<
"\n class C : " << sizeof(C) << "\n";
Run Code Online (Sandbox Code Playgroud)
结果是
bool : 1
int : 4
class A : 4
class B : 1
class C …Run Code Online (Sandbox Code Playgroud) 我想将Eigen :: ArrayXXd类型的数组移动(或交换)到Eigen :: MatrixXd.为此,我试过,
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main(int , char** ) {
Eigen::ArrayXXd array(100,100);
auto mat2 = std::move(mat.matrix());
cout << array.size() << endl;
cout << mat.size() << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出显示两个大小都是10000,这意味着复制了数组.为了避免复制,我也尝试过,
Eigen::MatrixXd mat;
mat.swap(array.matrix()); (runtime error assert failure)
// swap(array.matrix(), mat); (compile error)
Run Code Online (Sandbox Code Playgroud)
测试的Eigen I版本为3.2.0 beta1,使用gcc 4.8.0.从实验来看,似乎Matrix和Arrays的移动语义尚未实现.是对的吗?
有没有办法可以安全地移动(没有副本)?
我正在考虑使用std::queue(with std::deque)FIFO结构.
在队列数据结构中,数据仅在后面推送并在前面弹出.因此,弹出元素后前面的内存永远不会被使用.
我记得std :: vector在我显式调用shrink_to_fit方法之前不会释放内存.怎么样std::deque?
那么,我应该考虑释放前面永远不会再使用的内存吗?
首先,我构造了四个结构,每个结构返回值,l值引用,const l值引用,r值引用.我使用他们的包装(B或C),在方法func()的包装的,我想保持引用和CV预选赛func()的 A.
在c ++ 11中,我使用了尾随返回类型.但是随着c ++ 14中普通返回类型推导的到来,我猜我可以跳过尾随部分,但只有auto,返回类型忽略限定符和引用就像普通的一样auto.
那么,我的问题是在c ++ 14中实现它的最佳方法是什么,它的行为就像B下面的类一样?当它是微不足道的时候写尾随部分(通常是decltype(返回表达式))有时是令人沮丧的.
struct A1 {
int func(){
return x;
}
int x{3};
};
struct A2 {
int& func(){
return x;
}
int x{3};
};
struct A3 {
const int& func(){
return x;
}
int x{3};
};
struct A4 {
int&& func(){
return std::move(x);
}
int x{3};
};
template <class A>
struct B{
auto func() -> decltype(std::declval<A>().func()) …Run Code Online (Sandbox Code Playgroud)