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 ;)
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+?
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.
I do admit that I need to update myself with what GCC 6.0 allows vs the letter of the TS.
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#.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.
C++ templates are Turing complete, you can validate whatever you feel like at compile time.