我在类中有一些容器,例如vector或map,它包含生活在堆上的对象的shared_ptr.
例如
template <typename T>
class MyExample
{
public:
private:
vector<shared_ptr<T> > vec_;
map<shared_ptr<T>, int> map_;
};
Run Code Online (Sandbox Code Playgroud)
我希望有一个这个类的公共接口,有时会将shared_ptrs返回给const对象(via shared_ptr<const T>
),有时shared_ptr<T>
我允许调用者改变对象.我想要逻辑const正确性,所以如果我将方法标记为const,它就不能更改堆上的对象.
问题:
1)我对shared_ptr<const T>
和的可互换性感到困惑shared_ptr<T>
.当有人经过一个shared_ptr<const T>
shared_ptr的进级,做我把它存储为一个shared_ptr<T>
或shared_ptr<const T>
载体内,映射或修改我的地图,矢量类型(例如insert_elemeent(shared_ptr<const T>
OBJ)?
2)如下实例化类更好MyExample<const int>
吗?这看起来过于严格,因为我永远无法回归shared_ptr<int>
?
我有一个typedef
typedef unsigned int my_type;
Run Code Online (Sandbox Code Playgroud)
在文件中使用.我想让它在我的所有文件中都可见,而不是把它放在一切包含的头文件中.我不想去头文件路由,因为它代表这将是头文件中的唯一声明(并且似乎没有必要为此添加文件).
有没有办法做到这一点?
如果相反,我有:
typedef X my_type;
Run Code Online (Sandbox Code Playgroud)
其中X是一个类,我是否需要在任何地方包含Xh并在Xh结束时使用typedef?
我需要打开一个文件作为ofstream并写入文件的前面,同时保留文件的剩余内容,这将被"移动".类似于"prepend"文件.
这可能是使用STL还是提升?
shared_ptr的相等运算符定义如下:
template<class T, class U> inline bool operator==(
shared_ptr<T> const & a, shared_ptr<U> const & b)
{
return a.get() == b.get();
}
Run Code Online (Sandbox Code Playgroud)
这似乎破了.把这种平等转移到a和b所指的是不是更好?或者这是对图书馆用户的不公平限制(因为他们必须提供一个平等运营商)?
如果我有一个包含shared_ptrs的map或hash_table,那么当前定义会使等式无法使用.例如,考虑一下
std::map<int, std::tr1::shared_ptr<T> > m1, m2;
Run Code Online (Sandbox Code Playgroud)
我们不想检查m1和m2中每个int的ptrs是否指向相同的值?
我可以通过展平m1,m2 out来实现我自己的相等性(从每个构造集合,沿途取消引用shared_ptrs).是否有一个STL技巧可以完成这个或其他方式在shared_ptrs存在的情况下整齐地测试相等性?
我想沿着空格分割一个字符串,我知道标记代表有效的整数.我想将标记转换为整数并用它们填充向量.
我可以使用boost :: split,创建一个令牌字符串的向量,然后使用std :: transform.
你的解决方案是什么?使用boost是可以接受的.
我有一个模板化的类A <T,int>和两个typedef A <string,20>和A <string,30>.如何覆盖A <string,20>的构造函数?以下不起作用:
template <typename T, int M> class A;
typedef A<std::string, 20> one_type;
typedef A<std::string, 30> second_type;
template <typename T, int M>
class A {
public:
A(int m) {test= (m>M);}
bool test;
};
template<>
one_type::one_type() { cerr << "One type" << endl;}
Run Code Online (Sandbox Code Playgroud)
我希望类A <std :: string,20>做一些其他类没有的东西.如何在不更改构造函数的情况下执行此操作A:A(int)?
假设我有一个文件Xh,它定义了一个类X,其方法在X.cc中实现.文件Xh包含一个文件Yh,因为它需要Y来定义类X.在X.cc中,我们可以引用Y,因为Xh已经包含了Yh我是否还应该在X.cc中包含Yh?
我知道我不需要,我可以依靠标题保护来防止多重包含.但一方面,包括Yh使得X.cc更加独立于Xh(当然不能完全独立).什么是公认的做法?
另一个例子:包括<iostream>
.h和.cc文件.我看到有些人这样做,有些则没有.
以下代码:
using namespace std;
template <typename X>
class Goo {};
template <typename X>
class Hoo {};
template <class A, template <typename> class B = Goo >
struct Foo {
B<A> data;
void foo1();
void foo2();
};
template <typename A>
void Foo<A>::foo1() { cout << "foo1 for Goo" << endl;}
int main() {
Foo<int> a;
a.foo1();
}
Run Code Online (Sandbox Code Playgroud)
给我一个编译器错误:
test.cc:18: error: invalid use of incomplete type 'struct Foo<A, Goo>'
test.cc:11: error: declaration of 'struct Foo<A, Goo>'
Run Code Online (Sandbox Code Playgroud)
为什么我不能部分专门化foo1()?如果不是这样,我该怎么做?
我有另一个问题:如果我只想为A = int,B = Hoo定义foo2()而不是任何其他组合,我该怎么做?
我继承了现有的代码库,其中"功能"如下:
我正在尝试清理和重构代码,让它比我发现的更好一些.所以我的问题
使用gdb调试C++程序有哪些你最喜欢的技巧?
对所有技巧感兴趣但也
如何在gdb中调用对象上的方法(可能是虚拟的)
检查STL对象(漂亮打印它们)
使用continue防止gdb进入STL代码
处理内联,线程,tcmalloc(或自定义分配器)
在不同的会话中保留gdb命令的历史记录