我需要做一个像这样的typedef.
template< class A, class B, class C >
class X
{
};
template< class B, class C >
typedef X< std::vector<B>, B, C > Y;
Run Code Online (Sandbox Code Playgroud)
我刚刚发现它在C++中不受支持.有人可以告诉我如何通过替代手段实现同样的目标吗?
谢谢,Gokul.
我有一个带有模板化构造函数的非模板类.这段代码为我编译.但我记得在某个地方我提到构造函数不能是模板.有人可以解释这是否是有效的用法?
typedef double Vector;
//enum Method {A, B, C, D, E, F};
struct A {};
class Butcher
{
public:
template <class Method>
Butcher(Method);
private:
Vector a, b, c;
};
template <>
Butcher::Butcher(struct A)
: a(2), b(4), c(2)
{
// a = 0.5, 1;
// b = -1, 1, 3, 2;
// c = 0, 1;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Gokul.
我有一些C代码,其中有两个链表(比如A和B),A在特定位置插入B,A仍然有元素.
如何使用C++ STL有效地模拟相同的行为?如果我尝试拼接,它会使第二个空.
谢谢,Gokul.
在boost multi-index中,我可以通过元编程验证特定索引类型是否有序吗?有序索引,哈希索引,序列索引等.我可以通过元编程找到它们吗?
假设有一个像这样的索引:
int main()
{
typedef multi_index_container<double> double_set;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道double_set索引是排序还是散列或排序.当然在这种情况下,它是订购的.
我有一个抽象基类A和一组10个派生类.中缀运算符在所有派生类中都被重载
class A{
public:
void printNode( std::ostream& os )
{
this->printNode_p();
}
protected:
virtual void printNode_p( std::ostream& os )
{
os << (*this);
}
};
Run Code Online (Sandbox Code Playgroud)
有一个存储基类指针的容器.我想使用boost :: bind函数在每个派生类中调用重载的中缀运算符.我写得像这样
std::vector<A*> m_args
....
std::ostream os;
for_each( m_args.begin(), m_args.end(), bind(&A::printNode, _1, os) );
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?在visual studio中,我收到这样的错误
错误C2248:'std :: basic_ios <_Elem,_Traits> :: basic_ios':无法访问类'std :: basic_ios <_Elem,_Traits>'中声明的私有成员
谢谢,Gokul.
我正在尝试替换全局运算符new和delete.在Linux中,这工作正常,但在Windows(MSVC 10)中,它有时使用operator new的系统版本进行分配,然后尝试使用operator delete删除它.由于我在分配时存储了一些上下文信息,因此我的运算符delete在重新分配期间期望相同.我如何确保窗户始终保持我的操作员新功能?
编辑:我尝试了各种各样的东西.这些是声明
//Global new and delete overload
void* operator new (std::size_t bytes) throw(...);
void operator delete(void* p) throw();
void* operator new( std::size_t size, const std::nothrow_t&) throw();
void operator delete( void* mem, const std::nothrow_t&) throw();
void* operator new[] ( std::size_t bytes) throw(...);
void operator delete[](void* p) throw();
void* operator new[]( std::size_t size, const std::nothrow_t&) throw();
void operator delete[](void* mem, const std::nothrow_t&) throw();
#ifdef WIN32
void *__CRTDECL operator new(std::size_t size) _THROW1(_STD bad_alloc);
#endif
Run Code Online (Sandbox Code Playgroud)
这些是定义
#ifdef WIN32
void *__CRTDECL operator …Run Code Online (Sandbox Code Playgroud) 我想替换一个
#define INTERVAL_MASK(b) (1 << (b))
Run Code Online (Sandbox Code Playgroud)
具有内联函数.
int INTERVAL_MASK(int b)
{
return (1 << b);
}
Run Code Online (Sandbox Code Playgroud)
但我有一个switch case,它在case语句中使用预处理器指令.怎么去转换呢?更换switch搭配if是唯一的选择吗?
说,我有一个函数,它返回一个引用,我想确保调用者只将它作为引用,不应该作为副本接收它.这在C++中是否可行?
为了更清楚.我有这样的课.
class A
{
private:
std::vector<int> m_value;
A(A& a){ m_value = a.m_value; }
public:
A() {}
std::vector<int>& get_value() { return m_value; }
};
int main()
{
A a;
std::vector<int> x = a.get_value();
x.push_back(-1);
std::vector<int>& y = a.get_value();
std::cout << y.size();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Gokul.
我不知道为什么这个简单的代码不起作用.有人可以解释一下吗?
int main()
{
const char* c = "ret";
typedef unsigned char GOK_UINT8;
typedef GOK_UINT8* pGOK_UINT8;
const pGOK_UINT8 y = reinterpret_cast<const GOK_UINT8*>(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么reinterpret_cast应该不起作用?
谢谢,Gokul.
c++ ×9
boost ×2
templates ×2
bind ×1
constructor ×1
linked-list ×1
multi-index ×1
new-operator ×1
reference ×1
stdlist ×1
stl ×1
typedef ×1