Serving the Quantitative Finance Community

 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

template template template parameters

October 10th, 2013, 12:29 pm

Milo & Cuch: that code won't compile with Clang or GCC either (tested with development revisions, Clang 3.4 and GCC 4.9), since it's wrong.Recall that std::vector class template has *two* template parameters -- T and Allocator: http://en.cppreference.com/w/cpp/container/vectorSo, we have to take that into account: template <template <typename, typename> class Container, typename T>T statistic(Container<T, std::allocator<T>> const & container)// http://ideone.com/uCntM9Alternatively, we may use variadic TTP (clearly, non-variadic TTP is not templatey enough ;]) and stick the unused parameter(s) into a parameter pack ("Ts" in the following) as such:template< template <typename, typename...> class Container, typename T, typename... Ts>T statistic(Container<T, Ts...> const& container)// http://ideone.com/uFRlaXIs this generic? NO!Consider a container with (one or more) non-type template parameter(s), like, say, std::array.Here's what happens if you (incorrectly) attempt to treat this non-type template parameter as if it were a type template parameter:[0] TTP: http://ideone.com/9X9yZE[1] VTTP: http://ideone.com/jaQ4ohMy proposed solution would be to KISS:template <typename Container>typename Container::value_type statistic(Container const & container)// http://ideone.com/85poIGUpsides? It works and it's (relatively) generic.Downsides? Compile-type duck typing, so perhaps a little too generic for some (depends on taste) (but this is solvable via type template constraints - with traits & static_assert today and with concepts tomorrow); a bit on the verbose side all with having to type "typename Container::value_type" (that will go away with return-type deduction in C++14; note, however, that it's already shorter than any of the TTP alternatives).Exercise for the TTP-for-the-sake-of-TTP enthusiasts: fix [0] & [1] :>
Last edited by Polter on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 12:44 pm

C++ was not really meant for mathematical data structure like VectorSpace<V, K>. Goal: encapsulate common code! mind you, it has been tried (page 55)
Last edited by Cuchulainn on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 1:39 pm

QuoteMy proposed solution would be to KISS:template <typename Container>typename Container::value_type statistic(Container const & container)Yes, BUT STL does it this way and you can get funny things (Container is just some amorphous generic blob) (In C# I can do this either by 1) dynamic or 2) emit code; it works but it is mathematically not even wrong)
Last edited by Cuchulainn on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

template template template parameters

October 10th, 2013, 2:15 pm

QuoteQuoteMy proposed solution would be to KISS:template <typename Container>typename Container::value_type statistic(Container const & container)Yes, BUT STL does it this way and you can get funny things (Container is just some amorphous generic blob) No, in this case you can't. Class templates aren't function templates, and function templates aren't class templates.There are actually cases (when "the dependencies between the members and the type parameters of a generic class should be minimized") where being "just some amorphous generic blob" is exactly the right thing to do (amounts to ignoring internal implementation details irrelevant to the case at hand), see: http://blogs.msdn.com/b/vcblog/archive/ ... .aspxWhere it isn't, just constrain the type as desired (that's what static_assert is for).Quotebetter in this case!No, it's worse in this case ("statistic" function), since the TTP solutions (as proposed so far) won't work.The completely unrelated case of manually instantiating wrong allocator parameters for class templates is not relevant here, since we're talking about function templates (where type deduction, and thus instantiation, are fully automatic).
Last edited by Polter on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 2:52 pm

QuoteThere are actually cases (when "the dependencies between the members and the type parameters of a generic class should be minimized") where being "just some amorphous generic blob" is exactly the right thing to do (amounts to ignoring internal implementation details irrelevant to the case at hand), And that's exactly what have difficulty with. In C# I can constrain .... where "Container implements ISomething" // static_assert is a brutal one! Effective nonetheless
Last edited by Cuchulainn on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 3:06 pm

Is this codestack<int, vector<double>>stack<double, vector<int>>1. OK2. Wrong3. SafeI find it ambiguous at best and disastrous at worst.
 
User avatar
MiloRambaldi
Topic Author
Posts: 1
Joined: July 26th, 2010, 12:27 am

template template template parameters

October 10th, 2013, 3:47 pm

QuoteOriginally posted by: PolterMilo & Cuch: that code won't compile with Clang or GCC either (tested with development revisions, Clang 3.4 and GCC 4.9), since it's wrong.Thanks for spotting it. I checked the code with codepad, where it actually compiled and ran with no warnings.However, my original examples in this thread demonstrate the failure of TTP in VS (they have been checked with gcc). In the project I mentioned, VS failed miserably when TTTP was used; I forget exactly what the problem was.QuoteIs this generic? NO!Consider a container with (one or more) non-type template parameter(s), like, say, std::array.Here's what happens if you (incorrectly) attempt to treat this non-type template parameter as if it were a type template parameter:[0] TTP: http://ideone.com/9X9yZE[1] VTTP: http://ideone.com/jaQ4ohFresh TTP hell QuoteMy proposed solution would be to KISS:template <typename Container>typename Container::value_type statistic(Container const & container)// http://ideone.com/85poIGUpsides? It works and it's (relatively) generic.Downsides? Compile-type duck typing, so perhaps a little too generic for some (depends on taste) (but this is solvable via type template constraints - with traits & static_assert today and with concepts tomorrow); a bit on the verbose side all with having to type "typename Container::value_type" (that will go away with return-type deduction in C++14; note, however, that it's already shorter than any of the TTP alternatives).Yes, that was essentially my proposal for using boost::range (which confers other advantages, maybe not relevant to this discussion). In boost::range and STL they seem to prefer the more general solution:template<typename Container, typename T>T statistic(Container const& container, T init);http://ideone.com/PwtHmU.
 
User avatar
MiloRambaldi
Topic Author
Posts: 1
Joined: July 26th, 2010, 12:27 am

template template template parameters

October 10th, 2013, 4:22 pm

QuoteOriginally posted by: CuchulainnC++ was not really meant for mathematical data structure like VectorSpace<V, K>. Goal: encapsulate common code! mind you, it has been tried (page 55)looks fine to me...
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 5:23 pm

QuoteOriginally posted by: MiloRambaldiQuoteOriginally posted by: CuchulainnC++ was not really meant for mathematical data structure like VectorSpace<V, K>. Goal: encapsulate common code! mind you, it has been tried (page 55)looks fine to me...As specification? Does it run? Matematically speaking,A linear (vector space) is not necessarilly good as a specialisation of an Abelian (commutative) group. It's not even wrong. The + in Abelian groups is generic (aka can be anything) while + in vector spaces has a geometic meaning (n-d space). IMO better to call a vector space a set with + and * operations.
Last edited by Cuchulainn on October 9th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
MiloRambaldi
Topic Author
Posts: 1
Joined: July 26th, 2010, 12:27 am

template template template parameters

October 10th, 2013, 6:19 pm

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: MiloRambaldiQuoteOriginally posted by: CuchulainnC++ was not really meant for mathematical data structure like VectorSpace<V, K>. Goal: encapsulate common code! mind you, it has been tried (page 55)looks fine to me...As specification? Does it run? Matematically speaking,A linear (vector space) is not necessarilly good as a specialisation of an Abelian (commutative) group. It's not even wrong. The + in Abelian groups is generic (aka can be anything) while + in vector spaces has a geometic meaning (n-d space). IMO better to call a vector space a set with + and * operations.I had just enough interest to look at it briefly. Definitely not try to find code and run it!However, I believe that his AdditiveAbelieanGroup concept is a refinement of Abelian group specifying + as the group operation. It is perfectly correct to make (V,+) is an Abelian group part of the specification of (V,+,*) is a vector space.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 10th, 2013, 6:45 pm

QuoteOriginally posted by: MiloRambaldiQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: MiloRambaldiQuoteOriginally posted by: CuchulainnC++ was not really meant for mathematical data structure like VectorSpace<V, K>. Goal: encapsulate common code! mind you, it has been tried (page 55)looks fine to me...As specification? Does it run? Matematically speaking,A linear (vector space) is not necessarilly good as a specialisation of an Abelian (commutative) group. It's not even wrong. The + in Abelian groups is generic (aka can be anything) while + in vector spaces has a geometic meaning (n-d space). IMO better to call a vector space a set with + and * operations.I had just enough interest to look at it briefly. Definitely not try to find code and run it!However, I believe that his AdditiveAbelieanGroup concept is a refinement of Abelian group specifying + as the group operation. It is perfectly correct to make (V,+) is an Abelian group part of the specification of (V,+,*) is a vector space.If you do this then things get messy, in particular, the axioms for groups percolating down to vector spaces, e.g. existence of identity element and inverse, which vs don't have. IMO is wrong. GS has the most common definition:
Last edited by Cuchulainn on October 10th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

October 11th, 2013, 5:51 am

QuoteThe refinement from AdditiveAbelianGroup is less critical because the demand for additivity of the Vector type with all its properties is more obvious for VectorSpaces.Nevertheless, this is a design decision and other programmers might prefer to request AdditiveAbelianGroup in a where clause instead of a refinement.Indeed, it's design, not mathematics. Not an issue so long as we know the difference. BTW I would not even use 'where' for this.I already phrased it in a similar veinQuoteA linear (vector space) is not necessarilly good as a specialisation of an Abelian (commutative) group. It's not even wrong. The operation + in Abelian groups is generic (aka can be anything) while operation + in vector spaces has a geometic meaning (n-d space). I would say define a Commutativity concept and let AbelianGroup, VS or whatever space and then refine it or 'where' it. This would promote loose coupling and extendibility.
Last edited by Cuchulainn on October 10th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

template template template parameters

April 5th, 2015, 6:05 am

hihi!template template in C++11 -> C++17 Much beter
Last edited by Cuchulainn on April 4th, 2015, 10:00 pm, edited 1 time in total.