社区
C语言
帖子详情
能否给我讲解一下typedef和typedef typename的区别,以及什么时候应该使用哪一个?
AeoLusFeng
2002-03-27 09:38:38
看了很多书籍,讲解都不是很清楚,所以希望各位高手能够赐教!谢谢
...全文
1380
10
打赏
收藏
能否给我讲解一下typedef和typedef typename的区别,以及什么时候应该使用哪一个?
看了很多书籍,讲解都不是很清楚,所以希望各位高手能够赐教!谢谢
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lizmei001
2002-03-28
打赏
举报
回复
帮你UP
cococut
2002-03-28
打赏
举报
回复
typedef 最复杂的用法就是定义函数指针,举下面的例子:
#include <iostream.h>
#include <stdlib.h>
extern void func1(); // Declare 4 functions.
extern void func2(); // 在别的地方定义
extern void func3();
extern void func4();
typedef void (*pvfn)(); // function that takes no arguments
// and returns type void.
void main( int argc, char * argv[] )
{
// Declare an array of pointers to functions.
pvfn pvfn1[] = { func1, func2, func3, func4 };
// Invoke the function specified on the command line.
if( argc > 0 && *argv[1] > '0' && *argv[1] <= '4' )
(*pvfn1[atoi( argv[1] ) - 1])();
}
AeoLusFeng
2002-03-28
打赏
举报
回复
恩,看来我是给说了很多人的疑问,呵呵,表扬一下自己
不过分还是照给!感谢赐教!!!
mis98ZB
2002-03-28
打赏
举报
回复
哈哈,学到了!
hcpp
2002-03-27
打赏
举报
回复
受教了!!!
cococut
2002-03-27
打赏
举报
回复
typedef:是用于定义类型用的
1,为了简化,清晰。比如,
vector<list<int *>*> temp(10);
可以简化为
typedef list<int *> listnum;
typedef vector<listnum *> vectornum;
vectornum temp(10);
2,定义指向成员的指针。
class A{
virtual sup() = 0;
}
typedef void (A::* pt)();
void f(A *a)
{
pt ptemp = &A::sup;
}
typename:
template的含义有两个:
1)typename var_name;表示var_name的定义还没有给出,这个语句通常出现在模版的定义内,例如:
template <class T>
void f() {
typedef typename T::A TA; // 声明 TA 的类型为 T::A
TA a5; // 声明 a5 的类型为 TA
typename T::A a6; // 声明 a6 的类型为 T::A
TA * pta6; // 声明 pta6 的类型为 TA 的指针
}
因为T是一个模版实例化时才知道的类型,所以编译器更对T::A不知所云,为了通知
编译器T::A是一个合法的类型,使用typename语句可以避免编译器报错。
2)template < typename var_name > class class_name; 表示var_name是一个类型,
在模版实例化时可以替换任意类型,不仅包括内置类型(int等),也包括自定义类型class。
这就是问题中的形式,换句话说,在template<typename Y>和template<class Y>中,
typename和class的意义完全一样。
marconi
2002-03-27
打赏
举报
回复
to cococut(往事如屁!!!):能否将typedef的使用详细讲一讲。我还不太明白比如定义函数指针,指针数组之类的typedef用法。
zfbt
2002-03-27
打赏
举报
回复
噢!我也知道了!
fixopen
2002-03-27
打赏
举报
回复
typedef orgtype newtype
当orgtype编译器不知道是一种类型时,用:
typedef typename orgtype newtype
最常见的用法是,在类里面typedef了一个newtype,在另一个地方使用时。
prototype
2002-03-27
打赏
举报
回复
cococut is all right.
just show some more examples:
class A; // let compiler know A is a class here. i may define it later.
typedef A A_type; // ok
typedef A::B AB_type; // error. the compiler doesn't 'B' is a type or something else.
typedef typename A::B AB_type; // ok. you tell the compiler B is really a type, so it will not complain.
class A {
public:
typedef int B; // i kept my promise to really define 'B' as type here.
...
};
每天学点C++(C++实例教程:教程+源码)
Typedef
.zip
C++实例教程:教程+源码.zip。免费提供具体的C++例程,可自行下载学习!!
THE BOOST C++ METAPROGRAMMING LIBRARY
This paper describes the Boost C++ template metaprogramming library (MPL), an extensible compile-time framework of algorithms, sequences and metafunction classes. The library brings together important abstractions from the generic and functional programming worlds to build a powerful and easy-to-use toolset which makes template metaprogramming practical enough for the real-world environments. The MPL is heavily influenced by its run-time equivalent - the Standard Template Library (STL), a part of the C++ standard library [STL94], [ISO98]. Like the STL, it defines an open conceptual and implementation framework which can serve as a foundation for future contributions in the domain. The library's fundamental concepts and idioms enable the user to focus on solutions without navigating the universe of possible ad-hoc approaches to a given metaprogramming problem, even if no actual MPL code is used. The library also provides a compile-time lambda expression facility enabling arbitrary currying and composition of class templates, a feature whose runtime counterpart is often cited as missing from the STL. This paper explains the motivation, usage, design, and implementation of the MPL with examples of its real-life applications, and offers some lessons learned about C++ template metaprogramming.
高速对象池(内存池)类模板代码(C++)
包含定义代码和测试代码,代码不复杂,有注释,池对象存取时间复杂度为常数级,多线程测试速度可达千万次/秒。
使用
方法如下: 1.定义池,例如: FastPool
pool; 2.往池添加对象,例如: pool.Add("abc");//要注意这里传入的是构造对象的参数 3.取出
一个
对象以
使用
,例如: std::string* p = pool.Pop(); 这一步可以跟上一步结合起来: std::string* p = pool.AddAndPop("123");//这时候p就指向"123"这个刚加入池的std::string对象 4.存回对象以重用,例如: pool.Release(p); 5.还有的时候需要删除池中的对象,例如: pool.Delete(p);//只删除
一个
对象 pool.DeleteNotUsed();//删除池中未
使用
的对象 pool.DeleteAll();//删除池中所有对象 上面的示例只是模板参数为std::string 时的用法。还有一些函数未列出。希望代码能给大家带来启发。 重新检查了一次,发现模板代码中区分类和基本数据类型的代码用到了另外自定义的库。需要做如下修改: #include
//先定义
一个
模板用以区分类和基本数据类型的行为 template
struct ChangeClass {
typedef
typename
TT Type;}; template
struct ChangeClass
{
typedef
typename
std::vector
Type;}; 然后代码中的
typedef
typename
LK::Templates::UseT
::value, std::vector
, T>::type TP; if (LK::Templates::IsClassOrUnion
::value) 改为:
typedef
typename
ChangeClass
::value>::Type TP;//防止非类调用析构函数导致的错误 if (std::is_class
::value) 最后,欢迎喜欢编程的伙伴来qq群244953928探讨O(∩_∩)O
C++ Templates are Turing Complete
We sketch a proof of a well-known folk theorem that C++ templates are Turing complete. The absence of a formal semantics for C++ template instantiation makes a rigorous proof unlikely.
详解c++11新特性之模板的改进
主要介绍了详解c++11新特性之模板的改进,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C语言
70,037
社区成员
243,243
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章