Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Similarities and differences between Scala macros and C++ templates [closed]

Scala's Macros and C++ templates both provide access to compile time meta-programming. Could you elaborate on similarities and differences? Are they equal in terms of expressiveness?

like image 489
AndreasScheinert Avatar asked Oct 09 '12 11:10

AndreasScheinert


2 Answers

One important difference between them is that Scala macros are written in Scala, whereas C++ templates are their own programming language, which is completely unlike C++. C++ is an imperative object-oriented strict impure language, C++ templates are a declarative hybrid logic/functional non-strict pure language, which was never intended to be used as a full-fledged programming language, and thus lacks many of the features necessary for programming in the large.

like image 192
Jörg W Mittag Avatar answered Oct 20 '22 07:10

Jörg W Mittag


They both provide compile time metaprogramming and both are turing complete, but that's about all they have in common. I am no expert on C++, but as far as I know, the fact that C++ templates are turing complete is rarely exploited and using them for actual programming is hard. Often, templates are just used to provide parametric polymorphism (aka generics), whereas Scala macros are written in Scala and can use the full power of the language at compile time.

like image 38
Kim Stebel Avatar answered Oct 20 '22 08:10

Kim Stebel