Tag-Archive for » c++ «

星期三, 04月 29th, 2009 | Author: Zealot

问题是在这http://lilacbbs.com/forum/d.php?bid=66&id=62395,想了半天想不明白

摘要就是 Fred z = Fred(3);时没有执行拷贝构造函数,查了标准手册才发现原来如此:会不会调用拷贝构造函数由具体编译器实现决定:

Standard C++98 12.8

15 Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the

same cvunqualified

type, an implementation is permitted to treat the original and the copy as two different

ways of referring to the same object and not perform a copy at all, even if the class copy constructor or

destructor have side effects. For a function with a class return type, if the expression in the return statement

is the name of a local object, and the cvunqualified

type of the local object is the same as the function

return type, an implementation is permitted to omit creating the temporary object to hold the function return

value, even if the class copy constructor or destructor has side effects. In these cases, the object is

destroyed at the later of times when the original and the copy would have been destroyed without the optimization.

111) [Example:

class Thing {

public:

Thing();

~Thing();

Thing(const Thing&);

Thing operator=(const Thing&);

void fun();

};

Thing f() {

Thing t;

return t;

}

Thing t2 = f();

Here t does not need to be copied when returning from f. The return value of f may be constructed

directly into the object t2. ]

Category: C/C++, 程序设计  | Tags: , ,  | Leave a Comment
星期日, 04月 26th, 2009 | Author: Zealot

前不久需要解析一个json的接口,上json官网(http://www.json.org/json-zh.html)上找找C++parser。结果发现长长的list,支持各种语言的各种版本。上第一个官网看看(http://blog.beef.de/projects/tinyjson/),对tiny**的东西还是比较感兴趣的,比如tinyxml。。。

果然够tiny,下载下来一看发现只有一个.hpp文件就行了。接下来就开始ft了,按照官网上的sample调用了一下,编译都不过:

../include/tinyjson/tinyjson.hpp: In function ‘typename json::grammar<typename Iterator::value_type>::variant json::parse(const Iterator&, const Iterator&)’:

../include/tinyjson/tinyjson.hpp:549: error: expected `;’ before ’st’

../include/tinyjson/tinyjson.hpp:550: error: ’st’ was not declared in this scope

../include/tinyjson/tinyjson.hpp: In function ‘typename json::grammar<typename Iterator::value_type>::variant json::parse(const Iterator&, const Iterator&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]‘:

test.cpp:45: instantiated from here

../include/tinyjson/tinyjson.hpp:549: error: dependent-name ‘json::grammar<typename Iterator::value_type>::stack’ is parsed as a non-type, but instantiation yields a type

../include/tinyjson/tinyjson.hpp:549: note: say ‘typename json::grammar<typename Iterator::value_type>::stack’ if a type is meant

……

google了一下,相关资料真少,难道用c++解析json很发指么,最后硬着头皮在这个日文的bloghttp://d.hatena.ne.jp/S_Nakayama/20081207/1228592806)上找到了答案。修改记录如下:

549c549

< json::grammar< typename Iterator::value_type >::stack st;

> typename json::grammar< typename Iterator::value_type >::stack st;

565c565

< return json::grammar< typename Iterator::value_type >::variant(new boost::any());

> return typename json::grammar< typename Iterator::value_type >::variant(new boost::any());

相应的加上typename就行了。

最后,还想说一句,tinyjson官网的sample里遍历的方法也没编译过,最后改了一下遍历函数的参数,函数原型是void Traverse(json::grammar<char>::variant const v, const string& name, int level)。name表示上级节点名称,level表示当前内容的深度。虽然看着还是不爽,但好歹能工作了。

Category: C/C++, 程序设计  | Tags: , , , ,  | Leave a Comment