• 全部
  • 问答

模板,让我欢喜让我忧

anrxhzh 2002-07-02 05:09:22
//谁能告诉我
//这是为什么

#include <boost/thread/thread.hpp>
#include <iostream>

struct module{
void run();
};

struct module_entry{
module_entry(module&){};
void operator() (){};
};

int main()
{
module m;
boost::thread t2(module_entry(m));

module_entry e(m);
boost::thread t1(e);

std::cout << typeid(t1).name() << std::endl;
std::cout << typeid(t2).name() << std::endl;
}

//output:
//class boost::thread
//class boost::thread (__cdecl*)(struct module_entry)
...全文
15 点赞 收藏 24
写回复
24 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
daehappy 2002-07-07
up!gz!
回复
anrxhzh 2002-07-05
多谢指点,看来 ( declarator ) 并不是完全多余的。不幸的是这种语法会把模板搞得混乱(见原帖),使我只能曲线救国了(2002-7-3 16:12:07)。
回复
allen95 2002-07-05
gz
回复
prototype 2002-07-05
hehehe. if you don't think it is 灵活得过分 of declarations like:

int (*p)[100];
int (*f)( int );
// note the parentheses around the variable names.

then you probably should not think it is so of a delcarations like:

int (i);
int (((((j)))));
// without '[..]' / '(...)' after the last ')', this just declares a plain int object.

yes, this kind of syntax is valid, but useless and confusing. the pair of parenthesis here is redundant as it is redundant in an expression like:
int i = 0;
int j = (i); // no one would doubt the validity of the syntax (and semantics) here though there is a pair of useless and redundant parenthese.

btw, you can't have '.' and/or ' ' (white space) within a name. it is illegal no matter you have parentheses around the name or not.
回复
cloudwu 2002-07-05
我觉得 int (n) 没有什么过分的. 这使原生对象更像一个对象,
平常我经常这么用来着 :)
回复
prototype 2002-07-05
just looked into your original problem. actually it is not related to template. you can use

boost::thread t2 = module_entry(m);

or

boost::thread t2((module_entry)(m));

to avoid the ambiguity.

回复
anrxhzh 2002-07-04
还是查了一下,的确是符合标准的。这也灵活得过分了,害得我疑神疑鬼了两天,多亏我还有点道行,伙计们小心了!

http://www.cs.unc.edu/~smithja/C++Nov97/www.cygnus.com/misc/wp/nov97/decl.html#dcl.decl

4 Declarators have the syntax
declarator:
direct-declarator
ptr-operator declarator
direct-declarator:
declarator-id
direct-declarator ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationopt
direct-declarator [ constant-expressionopt ]
( declarator )
ptr-operator:
* cv-qualifier-seqopt
&
::opt nested-name-specifier * cv-qualifier-seqopt
cv-qualifier-seq:
cv-qualifier cv-qualifier-seqopt
cv-qualifier:
const
volatile
declarator-id:
id-expression
::opt nested-name-specifieropt type-name
A class-name has special meaning in a declaration of the class of that
name and when qualified by that name using the scope resolution opera-
tor :: (_expr.prim_, _class.ctor_, _class.dtor_).
回复
FCC 2002-07-04
gz
回复
anrxhzh 2002-07-04
懒得查了。看样子 int (n); 这样的语句是合法的。这有什么意义呢?反倒把boost::function的模板搞得一塌糊涂。唯一可能的用处是将来标识符可以有空格,比如可以定义一个名字为"my god"的整数:int (my god);太夸张了吧!
std::string (Mr. Smith);
(Mr. Smith) = "大坏蛋";
回复
anrxhzh 2002-07-04
//总算搞明白了
//待俺查一下标准

#include <iostream>

void foo(int(hello)=0)
{
std::cout << typeid(hello).name() << std::endl;
std::cout << typeid(foo).name() << std::endl;
}

int main()
{
foo();
}

//int
//void (__cdecl*)(int)

回复
LeeMaRS 2002-07-04
我总觉得这个问题和上次讨论那个临时对象,有一点关系.
回复
anrxhzh 2002-07-04
已经追加了100分了。
斑竹有义务回答这个问题,如果连C++语法都搞不定,大虾们还混个什么劲儿呀。
哎,非得俺威逼利诱才行吗:-)(-:
回复
anrxhzh 2002-07-03
是的,boost中的某些库是需要将cpp文件加入到项目中的,当然这些库也有编译好的library供下载,也可以自己生成library。
回复
Wargod2002 2002-07-03
谢谢大侠指点!
我以前下载了boost_1_28_0,用那个lexical_cast的时候好象只要
包含头文件就行了,难道这个还要用到lib吗或者加入cpp文件到项目吗?
回复
anrxhzh 2002-07-03
需要boost::thread库的源文件。
(下载地址ttp://www.boost.org/boost_all.zip)

源文件路径:
\boost_all\boost_1_27_0\libs\thread\src
回复
Wargod2002 2002-07-03
敢问大侠,为什么我在vc6下面无法链接通过这段代码,错误信息如下:
error LNK2001: unresolved external symbol "public: __thiscall boost::thread::~thread(void)" (??1thread@boost@@QAE@XZ)
error LNK2001: unresolved external symbol "public: __thiscall boost::thread::thread(class thread::function0<void,struct boost::empty_function_policy,struct boost::empty_function_mixin,int> const &)" (??0thread@boost@@QAE@ABV?$function0
@XUempty_function_policy@boost@@Uempty_function_mixin@2@H@1@@Z)
回复
hurrah_yule 2002-07-03
gz
回复
anrxhzh 2002-07-03
完整的代码在楼上:
回复人: anrxhzh(百宝箱) ( ) 信誉:110 2002-07-03 15:38:00 得分:0
回复
beijingshizi 2002-07-03
没有你的全部代码,看起来头大
回复
anrxhzh 2002-07-03
常在河边走,哪能不湿鞋。谁见过这种鬼画符?
struct test;
void x(test(hello));
回复
加载更多回复
相关推荐
发帖
C语言
创建于2007-09-28

6.3w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-07-02 05:09
社区公告
暂无公告