智圆行方
编程道路上的点点滴滴

1、如何消除"unused parameter"的警告:

http://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c-code

比较通用的做法就是

#define UNUSED(x) (void) (x)

USUSED(param);

但是我更欣赏gcc上的__attribute__,可惜MSVC不支持类似的语法。

 

2、关于C++中的反射机制:

http://stackoverflow.com/questions/582331/is-there-a-way-to-instantiate-objects-from-a-string-holding-their-class-name

Java和C#是支持反射的,但是C++却没有。这篇文章能够回答两个问题:1、为什么C++不支持反射;2、如何在一个C++应用中实现反射的功能。

 

3、关于“*** has virtual functions but non-virtual destructor”的警告

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1345226#pid9873107

如果基类中有虚函数,但未提供虚拟析构函数;在通过基类指针析构子类对象时,子类的析构函数不会被调用。

 

4、VS2005无法进行DEBUG:Binary was not build with debug information

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=927510&SiteID=1

原帖现在似乎已经无法打开,摘录具体操作步骤如下:

Its not an installation issue, the problem is, as the error message suggests, that you have not built your project with debug information.

To do this:
1) Goto Project->Properties
2) Make sure "Configuration" at the top is "Debug"
3) On the left, select "C/C++", then "General"
4) On the right, change "Debug information format" to "Program Database for edit and continue (/ZI)"
5) On the left, Select "Optimization"
6) On the right, Change "Optimization" to "Disabled (/Od)"
7) On the left, select "Code Generation"
8) On the right, change "Runtime library" to "Multi-Threaded Debug (/MTd)"
9) On the left, expand "Linker" and select "Debugging"
10) On the right, change "Generate Debug info" to "Yes (/DEBUG)"
11) Rebuild your project.

 

5、struct和typedef struct在C和C++中不同的含义:

http://www.cnblogs.com/qyaizs/articles/2039101.html

 

6、用extern "C" {}来区别C与C++:

http://weisjohn.blog.163.com/blog/static/310152562007102102637835/

这个知识在写SDK的时候有用,比方说我用C++写了个驱动,然后我在export include文件的时候希望C程序也能用,就得用到这个知识了。

 

7、C++中的回调机制:

http://user.qzone.qq.com/1379665549/blog/1404027783

这篇帖子是我在几篇帖子的基础之上整理的,比较杂乱,但是基本上全部涵盖了目前C++范围内所有的回调方式。个人认为应该还是boost或者C++11的function bind方式是最好的,稳定可靠。

 

8、从DLL中生成LIB:

http://user.qzone.qq.com/1379665549/blog/1402458873

这个适用于一些手上只有dll却需要把它链接到自己的程序中的情形,有点类似于hack技巧了。

 


9、从C++中调用C#编写的DLL:

http://user.qzone.qq.com/1379665549/blog/1399902711

说句实在的,我没搞懂究竟该怎么做,我也没真正的跑通过一个demo,只是整理下来吧,作为日后的参考。

 

10、C++的日志库整理:

http://blog.csdn.net/edychang/article/details/12507317

个人目前用的较多的是log4cxx。感觉已经够用了。

 

11、关于boost shared_ptr:

http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html

shared_ptr的优点在于线程安全和共享所有权。shared_ptr实在是非常强大的智能指针,C++程序员进阶工具:-)

 

12、关于boost::asio的socket编程:

http://www.cnblogs.com/TianFang/archive/2013/02/02/2890529.html

socket库目前倒是有不少选择,比如MFC的CAsyncSocket,Qt的socket库,但用起来总是颇多掣肘,我们公司的产品里面用的是boost的socket库,我想还是用这个比较靠谱。

目前还有比较火的ZMQ,这个并不是一个简单的socket封装,而是一个类似于消息队列的实现,对于同一进程间和网络节点间的网络通讯方式非常有用。这种结构上的清晰感会扩充编写网络应用的工程师的能力,让他们的创造力铺洒到更为广阔的领域中。

 

13、使用std::string来表示C++ byte array的利弊:

http://stackoverflow.com/questions/2037155/stdstring-as-c-byte-array

因为最近的代码里面自己实现了一个bytearray的封装,考虑到是不是可以用标准c的对象来实现这一目的。std::string本身可以做一个备选,std::vector<byte>也是。这篇帖子分析了下各自的优劣。

原本我的实现是用new byte[]来做的,后来比较了下,觉得std::vector<byte>更好。在内存管理上,避免了new/delete的直接操作,而且很多基本功能的实现只需要做个传递即可。

 

14、关于如何获取字符串格式的当前系统时间:

std::time_t t = std::time(NULL);
char mbstr[100];
std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", std::localtime(&t));

  

15、关于GCC中的一个编译警告:

1 bool success = concrete_operation();
2 assert(success == true);

在release模式下编译的时候,gcc会抛warning。这个警告是因为assert语句在release模式下会被优化掉(包括其中的函数调用!),要想解决这个问题,看下文:

http://stackoverflow.com/questions/5626429/assert-and-unused-local-variable-warning-in-gcc-dont-mix-well

 

16、关于strict aliasing:

http://blog.csdn.net/dbzhang800/article/details/6720141

类似于这样的code,

#if defined(__x86_64__)
    return *((size_t*)&double_);
#else
    {
      long long ll = *((long long*)&double_);
      size_t seed = ll & 0xFFFFFFFF;
      seed ^= (ll>>32) + (seed<<6) + (seed>>2);
      return seed;
    }
#endif

在gcc上面编译的时候会报dereferencing type-punned pointer will break strict-aliasing rules的警告。因为我尝试着把一个double类型的数转成size_t类型然后返回。

要注意的是,这边如果使用一个union来做转换,也是UB,并不会比原先的做法来的更安全。详见这篇文章:http://stackoverflow.com/questions/16637074/c-unions-vs-reinterpret-cast

个人认为比较靠谱的做法是用memcpy:

1 size_t ret;
2 memcpy(&ret, &double_, sizeof(double_));
3 return ret;

将double_转换成byte array然后进行拷贝,这个行为是可靠的。

 

posted on 2014-07-23 12:27  智圆行方  阅读(716)  评论(0)    收藏  举报