探索更多并找到答案以确定如何传递旧帖子(抱歉重复)
[原帖在下面]
我想总结一下按值传递、const 值、引用、const 引用、指针、const 指针的使用,请纠正我并给我您的建议。
还有一些问题:
根据juanchopanza 提到的“Want Speed? Pass by Value”一文,我再添加一项。
非常感谢!
在智能指针容器中添加项目的几种方法.我想知道你会选择哪种方式.
class MyContainer
{
private:
std::vector<std::unique_ptr<Item>> mItems;
public:
bool Add(Item* item);
// This is Way 1
//
// Advantages:
// - Easy to add derived items, such as Add(new DerivedItem);
// - No interface change if smart pointer type changes to such as shared_ptr;
//
// Disadvantages:
// - Don't explicitly show the item to add must be allocated on heap;
// - If failed to add, user has to delete the item.
bool Add(std::unique_ptr<Item> item);
// This is Way …Run Code Online (Sandbox Code Playgroud) 例如,我有一个班级
struct A {int a; bool b;};
Run Code Online (Sandbox Code Playgroud)
我想生成一个模板函数来获取它的元素(比如std :: get获取一个元组元素)
template<unsigned i, class T>
auto Get(T& t);
template<>
int& Get<0, A>(A& a)
{
return a.a;
}
template<>
bool& Get<1, A>(A& a)
{
return a.b;
}
int main()
{
A a;
Get<0>(a) = 10;
Get<1>(a) = true;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用.挑战在于我不知道Get for any class的返回类型.有没有办法实现呢?谢谢.
我上课了.它有unique_ptr的成员.
class A
{
std::unique_ptr<int> m;
};
Run Code Online (Sandbox Code Playgroud)
我希望它适用于以下陈述
A a;
A b;
a = std::move(b);
std::swap(a, b);
Run Code Online (Sandbox Code Playgroud)
怎么做?
根据评论,我有一个问题.这个编译器是否依赖?如果我什么都不做,它就无法通过VC++ 2012中的编译.
我之前尝试过
struct A
{
A() {}
A(A&& a)
{
mb = a.mb;
ma = std::move(a.ma);
}
A& operator = (A&& a)
{
mb = a.mb;
ma = std::move(a.ma);
return *this;
}
unique_ptr<int> ma;
int mb;
};
Run Code Online (Sandbox Code Playgroud)
但不确定这是否是最好和最简单的方法.
例如
struct A
{
vector<unique_ptr<int>> m_vector { make_unique<int>(1), make_unique<int>(2) };
};
Run Code Online (Sandbox Code Playgroud)
我尝试上面但失败了.有什么方法可以初始化unique_ptr的向量?
例如
template<typename T, typename... Ts>
struct Index
{
enum {value = ???}
};
Run Code Online (Sandbox Code Playgroud)
并且假设T是Ts中的一个,并且Ts具有不同的类型,例如
Index<int, int, double>::value is 0
Index<double, int, double>::value is 1
Run Code Online (Sandbox Code Playgroud) 我想构建一个模板函数来将std :: array转换为一个通用点,它有一个构造函数接受它的坐标参数.
template<typename PointT, size_t N>
PointT to(std::array<double, N> const& a)
{
return PointT(a[0], a[1], ...); // How to expand a?
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法扩展阵列a?
如何使用boost :: preprocessor解压缩一对对?
例如,我有一个如下序列(逗号之间无关紧要)
(int,x)(double,y)(float,z) or
(int,x),(double,y),(float,z) or
((int)(x))((double)(y))((float)(z))
Run Code Online (Sandbox Code Playgroud)
并希望转换为
int,double,float
Run Code Online (Sandbox Code Playgroud)
和
x,y,z
Run Code Online (Sandbox Code Playgroud)
通过使用macor之类的
UNZIP(i, seq)
Run Code Online (Sandbox Code Playgroud)
i索引在哪里
我正在使用doxygen来记录我的代码.我发现我可以很容易地找到所有类,因为有一个tab calles"classes".但我无法将所有自由功能列在一起.我必须转到选项卡"文件"来查找它们.我可以将所有免费功能放在doxygen文档中吗?
例如
struct Option_1
{
template<class T> using Vector = std::vector<T>;
};
Run Code Online (Sandbox Code Playgroud)
我可以
typename Option_1::Vector<int> v;
Run Code Online (Sandbox Code Playgroud)
但我更喜欢下面的
Vector<Option_1, int> v;
Run Code Online (Sandbox Code Playgroud)
或不带“typename”一词的类似名称。我定义了一个别名
template<class Option, class T> using Vector= typename Option::Vector<T>;
Run Code Online (Sandbox Code Playgroud)
但由于无法识别的模板声明/定义而失败。如何修复它?
c++ ×10
c++11 ×5
templates ×2
unique-ptr ×2
arrays ×1
boost ×1
containers ×1
doxygen ×1
type-alias ×1
using ×1