由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How does template work in C++
相关主题
C#说实际上更多是java拷贝C#概念。Go什么时候可能支持Generic?
《C# 5.0 核心技术》第5版[PDF]JAVA generic programming 是怎么实现的?
其实微软是个做语言的公司How to return an array in a C++ function?
a c++ question一个C的void指针的问题
问一个C++ template的问题C到底能走多远。。。。。。
每次转换数据,看到var就想杀人这个C#是为了啥?
array如何get set?现在还有人猛钻研c++模板编程,generic programming, 甚至meta-programming么 模板编程简单用用还可以 我现在工作中能用到一点点。不过也是属于组里大牛搭好了架子的 我
Java终于要支持primitive type的generic了C# is light-years ahead of Java now
相关话题的讨论汇总
话题: c++话题: template话题: java话题: does话题: type
进入Programming版参与讨论
1 (共1页)
h*********o
发帖数: 62
1
Java generic is totally different from C++ template under hood. Does C++
use also sharing and type erasure when dealling template? If not, do you
see any problem/benefit to do the template in a way that Java does? Or why
didn't java use C++ template to do its generics?
g*****g
发帖数: 34805
2
I'd take it this way. C++'s template is more flexible and powerful,
but too complicated and easily get abused, and make C++ pretty difficult
to learn and read at times.
Java generics, achieve about 90% of C++ template's power in a much simpler
way. Whether the other 10% is important is debatable. But so far, I haven't
heard a claim something can't be implemented in Java due to
this insufficiency.

why

【在 h*********o 的大作中提到】
: Java generic is totally different from C++ template under hood. Does C++
: use also sharing and type erasure when dealling template? If not, do you
: see any problem/benefit to do the template in a way that Java does? Or why
: didn't java use C++ template to do its generics?

a*****k
发帖数: 704
3
I think Java wants to be back compatible with its old code

why

【在 h*********o 的大作中提到】
: Java generic is totally different from C++ template under hood. Does C++
: use also sharing and type erasure when dealling template? If not, do you
: see any problem/benefit to do the template in a way that Java does? Or why
: didn't java use C++ template to do its generics?

r*********r
发帖数: 3195
4
c++ uses type expansion. java uses type erasure. exactly opposite.
c++'s way may lead to code bloating.
but c++'s template uses integers as type parameters,
which makes the mechanism Turing complete.
y*******g
发帖数: 6599
5

Can you explain in more detail? why integer as type parameters makes it
Turing complete
thanks

【在 r*********r 的大作中提到】
: c++ uses type expansion. java uses type erasure. exactly opposite.
: c++'s way may lead to code bloating.
: but c++'s template uses integers as type parameters,
: which makes the mechanism Turing complete.

r*********r
发帖数: 3195
6
因为所有的计算问题都可以归结为(reduce to)整数的加减乘除.
CPU 的核心是 ALU. ALU 能做的操作也就是整数的四则运算.
r*********r
发帖数: 3195
7
think of c++'s template mechanism as a functional language,
like Haskell or ML.
it has integer and boolean operations, also control flows like
selection and repetition ( actually recursion), and assignment
(with typedef and static constant integer definition).
think about the following piece of code. (copyrighted by me. :)
the real computation is done by the compiler.
#include
template
struct int_
{
static const int value = n;
};
template
struct plus
1 (共1页)
进入Programming版参与讨论
相关主题
C# is light-years ahead of Java now问一个C++ template的问题
C++ Primer 和 C++ Primer Plus有什么区别每次转换数据,看到var就想杀人
这次Go丢人有点大呀array如何get set?
C++11和clang的很多新特性本质就是纵容新手酒驾Java终于要支持primitive type的generic了
C#说实际上更多是java拷贝C#概念。Go什么时候可能支持Generic?
《C# 5.0 核心技术》第5版[PDF]JAVA generic programming 是怎么实现的?
其实微软是个做语言的公司How to return an array in a C++ function?
a c++ question一个C的void指针的问题
相关话题的讨论汇总
话题: c++话题: template话题: java话题: does话题: type