模板,让我欢喜让我忧

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)
...全文
53 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
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));
加载更多回复(4)

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧