c++
共 13 篇文章
pthread_rwlock_t 未定义的问题
最近在linux上次编译时出现pthread_rwlock_t没有定义的现象.但是已经include了pthread.h,网上查了一些资料后发现,原因竟然是因为显示使用了-std=c99参数导致. 在c99没有完全实现时,如果不显示指定-std, 则gcc默认使用gnu89(或gnu90)标准. 一般而言, gnuXX标准 = cXX标准 + GNU extension 而pthread_rwlock_t正好就是GNU extension的一部分. 所以指定使用c99就会把GNU extension禁用,造成pthread_rwlock_t未定义的问题. 解决方法就是: 方法一: 把-std…
二级指针 const 参数
http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html 写道 [18.17] Why am I getting an error converting a Foo** → Foo const**? Because converting Foo** → Foo const** would be invalid and dangerous. C++ allows the (safe) conversion Foo* → Foo const*, but gives an error if you try to implic…
tricks
#include <typeinfo> #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <list> using namespace std; template< template<typename, typename> class container_type, typename value_type> void foo(container_type<value_type, std::allocator<value_type> > const& container) { …
2012-12-31 14:21 · c++
多输出带前缀输出流
http://stackoverflow.com/questions/2212776/overload-handling-of-stdendl 写道 #include <iostream> #include <sstream> class MyStream: public std::ostream { // Write a stream buffer that prefixes each line with Plop class MyStreamBuf: public std::stringbuf { std::ostream& output; public: MyStreamBuf(std…
2012-10-29 11:37 · c++
inherit the ostream
class cdebug_stream :public ostream { public: typedef cdebug_stream& (*manip_fnptr)(cdebug_stream&); void attach(ostream& os) { m_outs.push_back(&os); } void detach(ostream& os) { m_outs.erase(std::remove(m_outs.begin(),m_outs.end(),&os),m_outs.end()); } template<typename T> cdebug_stream& operator…
2012-10-26 16:02 · c++
模板参数 函数指针
http://stackoverflow.com/questions/5507377/template-type-deduction-for-a-pointer-to-member-function 写道 For a.connect<double> (&GApp::foo), both the foo(double) and foo(double, double) will match the overload of connect with one, and with two template parameters respectively (in the case of the two …
2011-12-22 13:12 · c++
event
#ifndef ODBCLIB_CORE_EVENT_EVENT_H #define ODBCLIB_CORE_EVENT_EVENT_H #include "Event.template" #include "Event.implement" #endif #ifndef ODBCLIB_CORE_EVENT_EVENT_TEMPLATE #define ODBCLIB_CORE_EVENT_EVENT_TEMPLATE #include <vector> namespace odbclib { namespace core { namespace event { template <ty…
2011-08-19 14:42 · c++
c++ Delegate
#ifndef DUMMYCLASS_H #define DUMMYCLASS_H namespace delegate { class DummyClass{}; } #endif #ifndef METHODSTORAGE_H #define METHODSTORAGE_H #include "DummyClass.h" namespace delegate { class MethodStorage { public: typedef DummyClass GenericClass; typedef void (GenericClass::*GenericMemberFunctionP…
2011-08-06 19:59 · c++
Nullable (bug)
#ifndef NULLABLE_H #define NULLABLE_H #include "odbclib.h" namespace odbclib { template<typename T> class Nullable { typedef T const& const_reference; typedef T const* const_pointer; public: Nullable() :m_value(T()), m_isNull(true) { } Nullable(T const& value) :m_value(value) { } bool isNull() cons…
2011-06-26 23:45 · c++ · C++ · Nullable
C++ 接异常时的注意
void test1() { try { try { throw runtime_error("asdfasdf"); } catch(std::exception &e) { throw e; } } catch(std::runtime_error &e) { throw e; } catch(std::exception &e) { cout<<e.what()<<endl; } } 这段代码都有输出: 在vc2010里输出 仍然是 asdfasdf 但是mingw4.5.2里输出则是std::exception 当runtime_error 被扔出来的时候,exception& e接…
2011-06-24 23:55 · c++ · C++
类成员函数模板特化 默认参数
#ifndef MEMORYBLOCK_H #define MEMORYBLOCK_H #include "odbclib.h" #include <cstdlib> using std::min; namespace odbclib { class MemoryBlock { public: typedef unsigned char byte; explicit MemoryBlock(size_t = 0xff) throw(runtime_error); MemoryBlock(MemoryBlock const&) throw(runtime_error); virtual ~Me…
2011-06-19 11:23 · c++ · C++ · template member · specialization
关于抛出异常和清栈
构造函数中异常: 1.在无继承关系的前提下,构造函数中抛出异常未尝不可。 因为在对象没能构造完整的情况下,是不会去调用析构函数的。 2.在有继承关系的情况下,构造函数中抛出异常可能会引起问题 2.1如果基类中有纯虚函数,而且在基类的析构函数中有被调用的话,会得到一个 pure virtual function call的异常 2.2如果基类中有纯虚函数,但是基类的析构函数中没有被调用到,是没有关系的。 对2.1的猜想: 因为派生类构造函数在执行的时候,会先去调用基类的构造函数。所以如果异常产生于派生类,系统会认为基类已经构造完毕,但是这时因为派生类还没有构造完毕。又因为此时产生异常,系统需要…
2011-06-18 22:10 · c++ · C++ · constructor · Exception
类成员函数模板特化
//header file namespace odbclib { class MemoryBlock { public: typedef unsigned char byte; explicit MemoryBlock(size_t = 0xff) throw(runtime_error); MemoryBlock(MemoryBlock const&) throw(runtime_error); virtual ~MemoryBlock(); MemoryBlock& initBlock(byte); void const* getRawBlock() const; void* getR…
2011-06-12 20:20 · c++ · VC++