News for hackers. Stuff that matters.
Re:Swift 2.2 Released by pkolaczk on Tuesday March 22, 2016 @06:12AM (#11335465)
Templates are not proper generics. Templates can be used to emulate generics, but they are not the same thing at all. Templates are not first-class citizen of the type system (they don't survive to the typechecking phase), while generics are. You cannot write a generic list implementation in C++ and have it typechecked.
Re:Swift 2.2 Released by fauigerzigerk on Tuesday March 22, 2016 @08:11AM (#11335860)
I'd like to hear more about that because I'm not sure I understand. Can you give an example for something you cannot do in C++ in a type safe way that you can do in C#, Java or Swift?
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:20AM (#11343218)
I already gave one. You can't write a generic list implementation and make the compiler prove your code is sound (according to the rules of the C++ type system) for any valid type argument value.
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @07:38AM (#11343526)
You obviously need to learn about template metaprogramming, enable_if, type traits and SFINAE.

C++ templates are Turing complete, you can validate whatever you feel like at compile time.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @08:16AM (#11343714)
I'm absolutely aware of enable_if, type traits and SFINAE. But they do not address the concern I raised here. The things you mention come to play on template instantiation, after the concrete (non-generic) types are already known. The problem I mention is that with templates you cannot type check generic code. I can put whatever nonsense enable_ifs on my template declaration and the compiler does not check if they are satisfied by the implementation.

You can write the following C++ code and C++ allows it:

<template typename T> void foo(const T& arg) { arg.bar(); }

This is perfectly fine for a macro system. But this is not how truly generic type system would work. If templates were proper generics, the typechecker would immediately refuse this to compile saying something like: "Error: I can't prove T has the bar() member."

> C++ templates are Turing complete, you can validate whatever you feel like at compile time.

So please write a template metaprogram that validates if a given C++ class (input to the program, given as a template argument) matches a table schema in the database system (the table name and connection string are also given as template arguments). For fetching the schema, you have to connect to the DB, of course at compile time. If the schema does not match, the compilation should fail ;)

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @08:32AM (#11343816)
Because whoever wrote foo () was too lazy to make use of type traits and validate that T has a method called bar with zero arguments.

Do you want an example how to write it properly?

With or without the upcoming concepts TS already in GCC 6.0?

Also this example would require T to be an interface with a bar method in C#. Do you also want to the the variant with MI and pure abstract classes in C+?

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @09:36AM (#11344227)
> Because whoever wrote foo () was too lazy to make use of type traits and validate that T has a method called bar with zero arguments.

In a proper generic type system, the compiler validates it, not the programmer. Even if you constrain the type using type traits, the compiler would not check if the implementation satisfies the constraints. I can put a type trait for existence of bar() and then another programmer may rename bar() to baz() forgetting to update the constraint and the code will still compile fine.

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @10:19AM (#11344665)
Except that reasoning fails with concepts, already available in GCC 6.0 and as a TS.
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @11:23AM (#11345285)
So you're saying concepts will reject libraries that compile fine today?
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @01:04PM (#11346266)
They might, if used properly.

I do admit that I need to update myself with what GCC 6.0 allows vs the letter of the TS.

Re:Swift 2.2 Released by fauigerzigerk on Wednesday March 23, 2016 @05:45PM (#11348523)
I don't think concepts are even necessary to do what pkolaczk wants to do, which is (I think) to constrain the argument to a particular interface, say Bar:

  template <typename T> 
  void foo(const T& arg) {
    const Bar* b = arg;
    b->bar();
  }
or

  template <typename T> 
  void foo(const T& arg) {
    static_assert(is_base_of<Bar, T>::value, "T must be a Bar");
    arg.bar();
  }
I can't say I'm a huge fan of either syntax, but the compiler will perform static type checking as if you had written where T : Bar in C#.
Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:38PM (#11348840)
The body of the template won't be typechecked until the instantiation of the template. This is different than in C#. If I replaced arg.bar with arg.baz in C#, the generic type definition would not compile, even if I never use it. In C++ I can't write a template and have it typechecked for all possible input types satisfying the type constraints, in C# I can. So C++ type system is weaker than that of C#.
Re:Swift 2.2 Released by fauigerzigerk on Wednesday March 23, 2016 @07:12PM (#11349089)
I see what you mean, but uninstantiated also means unused. So we're not talking about anything that could possibly lead to runtime errors. It's not an issue with type safety.

It definitely has a downside for the development process, especially when it comes to writing libraries. But it is also to some degree inevitable if you want structural typing. Maybe there is a better way to do it though.

Re:Swift 2.2 Released by pjmlp on Tuesday March 22, 2016 @08:17AM (#11335895)
Now I am curious about an example where generics are able to do what templates, which are Turing complete, aren't.
Re:Swift 2.2 Released by chadzawistowski on Tuesday March 22, 2016 @06:36PM (#11340483)
I think of templates as an automated copy+paste mechanism. C++ templates can bloat the compiled executable pretty significantly... If you define a generic Foo<String> and Foo<Widget>, C++ templates will generate two separate binary implementations. C# will use the same implementation, and perform type checks using reflection at runtime.

Further support for the idea that C++ templates are an automated copy+paste mechanism is that you're allowed to give templates arguments that are not types.

Further reading: https://msdn.microsoft.com/en-us/library/c6cyy67b.aspx

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @02:02AM (#11342439)
It is not how you think of them, rather what is possible to do with their expressiveness.

C# type checks imply a performance hit and are an implementation detail. Nothing prevents a C# compiler to choose another one. It has nothing to do with generics vs templates.

For example Modula-3 and Ada compilers use the same approach as C++ for code generation of their generics.

So where is that example of something that can only be written with C# generics, but not with C++ templates?

A github gist maybe?

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:24AM (#11343233)
It is not the case of what can only be written, but what can be written and typechecked. C# generics offer much stronger type safety for library writers than C++ templates. C++ templates are not type-checked. C# (and Java, and Scala and Haskell) generics are.
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @07:40AM (#11343534)
Yes they are type checked.

That is what enable_if, type traits, if constexpr and eventually concepts allow for.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @10:14AM (#11344607)
No, they are not. This code compiles fine, despite an obvious non-generic type-error. You may add whatever number of type traits, constexprs, etc there, and it will still compile fine as long as syntax is ok.

  #include <string>

  template <typename T>
  void foo(const T& arg) {
    int x = std::string("int expected");
  }

  $ g++ -c template.cpp
  $ // see, no error
Templates are only syntax-checked. The type checker is not even executed on the non-generic code inside of templates. The type checker will be executed after the template is instantiated (expanded). At that point, generic types do not exist. There exist only concrete types.
Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @10:49AM (#11344958)
They are still type checked when they are instantiated and the code is as generic as always.

Uninstantiated templates are dead code that will be stripped anyway.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @11:11AM (#11345171)
Did I write anything different?

Sure, you can type-check them for particular type arguments. But you can't type-check them generally at the generic level and you have absolutely no guarantee that your library code is free of type errors. Not a problem if the goal is to produce the final executable, but a big problem for a library writer, who doesn't control the inputs (in this case: the types provided by the user of the library).

And AFAIK C++17 concepts do not address this problem, really. I can constrain my input types with them, and the calling code will be forced to conform to them (and the compiler will present a nice error message if it doesn't), but there is still no checking on the other side - that is if the template implementation is correct assuming these constraints. I can publicly declare my code requires T.bar(), then shamelessly call T.baz() in the template implementation and the compiler will not catch it.

This is like a Python program. You don't know if it type-checks before you run it, but even if you run it once or twice and it was fine, that doesn't guarantee there are no type errors.

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @01:27PM (#11346457)
Yes.

The code line int x = std::string("int expected"); has zero relation with any of the template arguments.

So a programmer error that doesn't have anything to do with the types provided for the template.

Sure it will be caught in C#, because its build model assumes the existence of modules and everything is compiled to binary.

In C++ a similar compilation error would occur when compiling the translation unit into a library where the template is instantiated.

Of course, most templates being header only the error will only occur when instating it, but it will still blow on compilation and prevent the final generation of the exe, dll being produced.

Also errors related to semantic type check, independent of template arguments is relatively easy to achieve with unit tests.

I still fail to see how a generic system that doesn't offer partial specialization or meta-programming as being more powerful than templates.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:50PM (#11348927)
I never said more powerful. I said stronger. In C++ you need unit tests to check something I can have statically proven in C#. And obviously you can't instantiate your template for all possible valid input types, because this number can be infinite.
Re:Swift 2.2 Released by tarblog on Thursday March 24, 2016 @01:43AM (#11350815)
Monomorphisation is the name of the technique of producing a different version of the code for each set of type parameters that are used in the program. C++ and Rust use monomorphisation instead of doing runtime checks because they're trading off slightly bigger binaries for greater speed.

By not doing runtime checks you get to skip that work at runtime, obviously, but you also get to enable other optimizations like inlining the monomorphised version.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @06:16AM (#11343196)
Templates are a macro system. Macro systems are often Turing-complete. But if being Turing-complete was the only important thing in programming, then we'd all code in assembly and never look forward. Most programming languages are Turing-complete, but this doesn't mean they are practically equivalent for every job. You know, Brainfuck is also Turing-complete, but you'd be probably pissed-off if I said heavy-templated C++ code readability is often close to that of Brainfuck.

Macro systems can be very powerful, but they are also quite hard to use. It is 2016 and error messages in C++ templates are still inferior to the messages you get in C#, Java or Scala generics, despite templates existing for much longer.

C++ templates are also not the very best at metaprogramming either. Take any macro system like the ones found in LISPs, Scala or Template Haskell - they are much more powerful and consistent with the rest of the language. I can code Scala metaprograms in Scala or LISP metaprograms in LISP. But I can't code C++ metaprograms in C++, I have to use this weird duck-typed functional template language which doesn't have even loops or conditionals.

Re:Swift 2.2 Released by pjmlp on Wednesday March 23, 2016 @07:44AM (#11343556)
That weird duck typed functional template language runs circles around C# and Java generics.

I have experience in compiler design and C#, Java and C++ are my tools of trade. So I know their generics systems pretty well.

Maybe if I get bored during Easter I can bother to provide your safe list.

Re:Swift 2.2 Released by pkolaczk on Wednesday March 23, 2016 @09:56AM (#11344435)
> That weird duck typed functional template language runs circles around C# and Java generics

This sentence doesn't make much sense. Comparing oranges and apples. It is like saying Scala macros are better than Scala generics. C++ templates are neither great at metaprogramming (there are much better macro systems in other languages out there) nor great at generics (Java, C# or Swift generics are not state-of-the-art either, but in some cases I mentioned they are better).