Concerning this question: What is the syntax for partially specialising a template based on the number of parameters a template template parameter takes?
Edit: The linked question has now been edited; to see what it looked like when I asked this question, please see the original, unedited version
As the aforementioned question is still unanswered, and my curiosity unsatisfied, I was planning on remodeling it to be more reader-friendly. I plan to place the concise demonstration of the issue, along with the question itself, at the top of the post instead of the bottom, and provide information about the specific case where the issue arose below it, for context (and so it can more easily be ignored, if one isn't interested in the context). [Note that the specific case itself was easily solved, by renaming are_trait
to are_unary_trait
and are_binary_trait
; I'm purely concerned with whether what I was attempting is or isn't legal C++ (and if not, whether it's a GCC extension or a bug).]
While doing so, however, I noticed that a significant portion of the post (3,814 characters out of 11,950, or roughly 32%) was a formatted copy-paste of the error messages emitted by Clang and MSVC when compiling the specific-case example code provided for context; this section significantly increases the vertical height of the post due to its formatting (my rough estimate places it at 3/8 of the post's total height). This information could more cleanly be provided with links to the example code on Coliru (for Clang) and on Rextester (for MSVC). In my opinion, the specific error messages, while helpful for their insight into how Clang & MSVC handle the code, aren't critical to the question; therefore, I would rather trim the post, while providing them in an easy-to-access way (which is also easy to replace if the links become outdated, by just copying the code to Coliru & Rextester again).
So, considering this, should I replace the error messages with those links to Coliru and Rextester, or keep them in all of their glory?
For comparison's sake, my intended edit is provided below. It reorders the content so that the question itself is more easily readable, and (IMO) presents the context more cleanly, while shortening the post to 10,111 characters (despite adding a cleaner version of the MCVE), and is only around 4/5 of the original's height; it has the downside, however, of requiring answerers to actually click the links if they're interested in the specific errors emitted by Clang and MSVC.
[All example code, other than the new MCVE at the top, is directly copied from the original post, apart from a comment that inadvertently messed up the error messages' line numbers. Some prose was directly copied, and some was reworded for brevity.]
Consider the following code:
template<typename>
struct One {};
template<typename, typename>
struct Two {};
template<template<typename...> class TTP, typename...>
struct SS;
#ifdef TEST_TTP
template<template<typename> class OneParam,
typename... Ts>
struct SS<OneParam, Ts...> {};
template<template<typename, typename> class TwoParam,
typename... Ts>
struct SS<TwoParam, Ts...> {};
#else // TEST_TTP
template<template<typename> class OneParam,
typename TParam>
struct SS<OneParam, TParam> {};
template<template<typename, typename> class TwoParam,
typename TParam1,
typename TParam2>
struct SS<TwoParam, TParam1, TParam2> {};
#endif // TEST_TTP
int main() {
SS<One, int> ssoi;
SS<Two, int, int> sstii;
}
This code will compile properly on Clang, GCC, and MSVC, if TEST_TTP
isn't defined. However, if it is defined...
- The code compiles properly on GCC, indicating that it recognises that
OneParam
andTwoParam
are distinct fromTTP
in the primary template. - Clang fails to recognise that
OneParam
specialisesTTP
, causing it to emit two errors (the first being that the partial specialisation doesn't specialise any template parameters, and the second being thatOneParam
conflicts with the previously-declared template template parameter). It then emits similar errors forTwoParam
(the first is identical, while the second says that the template template parameter has too many parameters), and an error for each instantiation ofSS
(because it considers the template to be undefined), for a total of 6 errors. - MSVC emits similar errors to Clang, but more concisely: It emits C3855 (
OneParam
is incompatible with the primary template), and a C2079 (variable uses undefined type) for each instantiation ofSS
, for a total of 3 errors.
From my testing:
GCC allows a template with a template template parameter which takes a variadic parameter pack to be partially specialised based solely on the number of parameters that template template parameter takes. Clang and MSVC do not.
template<template<typename...> class T> struct S;
template<template<typename> class T> struct S<T> {}; // Only works with GCC.
template<template<typename, typename> class T> struct S<T> {}; // Only works with GCC.
Clang and MSVC are fine with this if other parameters are also specialised, however.
template<template<typename...> class T, typename... Ts> struct S;
template<template<typename> class T,
typename TParam>
struct S<T, TParam> {};
template<template<typename, typename> class T,
typename TParam1,
typename TParam2>
struct S<T, TParam1, TParam2> {};
It would thus appear that either the former isn't legal C++, or it isn't properly supported by Clang and MSVC. So, the question is this:
Considering this, what is the proper, legal syntax for partially specialising a template, which contains a template template parameter, based on the number of parameters that template template parameter takes? If there is no legal syntax for this, is supporting it a GCC extension and/or bug?
Context: I was attempting to make a clean are_trait
wrapper to apply type trait checks to parameter packs, and came across an interesting situation where my solution compiles with GCC, but not Clang or MSVC; the latter two seem to be unable to detect the subtle nuance in the partial specialisation.
The test code used was:
#ifndef ARE_TRAIT_H
#define ARE_TRAIT_H
#include <type_traits>
// Based on SameComparison::are_same, by dyp ( https://stackoverflow.com/a/20047561/5386374 )
template<template<typename...> class Trait_t,
typename... Ts>
struct are_trait;
// One-parameter trait.
template<template<typename> class Trait_t,
typename... Ts>
struct are_trait<Trait_t, Ts...> : std::true_type {};
template<template<typename> class Trait_t,
typename T,
typename... Ts>
struct are_trait<Trait_t, T, Ts...>
: std::integral_constant<bool,
Trait_t<T>::value &&
are_trait<Trait_t, Ts...>::value> {};
// Two-parameter trait, compares all to first element after trait.
template<template<typename, typename> class Trait_t,
typename To,
typename... Froms>
struct are_trait<Trait_t, To, Froms...> : std::true_type {};
template<template<typename, typename> class Trait_t,
typename To,
typename From,
typename... Froms>
struct are_trait<Trait_t, To, From, Froms...>
: std::integral_constant<bool,
Trait_t<From, To>::value &&
are_trait<Trait_t, To, Froms...>::value> {};
#endif // ARE_TRAIT_H
int dummy() { return 0; }
int main() {
// Single-parameter trait:
static_assert(are_trait<std::is_pointer, int*, void*, decltype(&dummy)>::value,
"int*, void*, and decltype(&dummy) aren't all pointers.");
//static_assert(are_trait<std::is_pointer, int*, void*, int>::value,
// "int*, void*, and int aren't all pointers.");
// Two-parameter traits:
static_assert(are_trait<std::is_same, int, int, int>::value,
"int, int, and int aren't all int.");
//static_assert(are_trait<std::is_same, int, int, decltype(&dummy)>::value,
// "int, int, and decltype(&dummy) aren't the same.");
static_assert(are_trait<std::is_convertible, int, char, bool, float, long>::value,
"char, bool, float, and long aren't all convertible to int.");
//static_assert(are_trait<std::is_convertible, int, char, bool, float, decltype(nullptr)>::value,
// "char, bool, float, and nullptr_t aren't all convertible to int.");
}
On GCC, the above works as intended: The live checks all pass, and the commented-out checks all emit an error. However, Clang emits 6 errors, and MSVC emits 4 errors.
It was easy to make are_trait
work as intended, by renaming the one-parameter-ttp version are_unary_trait
and the two-parameter-ttp version are_binary_trait
, but it got me curious about the syntax in general: Whether it is or isn't legal C++, and if not, why GCC supports it.
It thus appeared that Clang and MSVC were unable to detect that the template template parameter is specialised, and instead see a conflicting declaration. To test this, I made a few test programs, and was able to replicate the behaviour.
// Test case #1: Short demonstration of the problem.
// Dummy structs.
template<typename T>
struct OneParam {};
template<typename T1, typename T2>
struct TwoParam {};
// Template template parameter & specialisations.
template<template<typename...> class TempTempParam>
struct TTP {
static constexpr int value = 0;
};
template<template<typename> class TempTempParam>
struct TTP<TempTempParam> : TempTempParam<int> {
static constexpr int value = 1;
};
template<template<typename, typename> class TempTempParam>
struct TTP<TempTempParam> : TempTempParam<int, int> {
static constexpr int value = 2;
};
int main() {
TTP<OneParam> ttp1;
TTP<TwoParam> ttp2;
// These fail with Clang & MSVC.
static_assert(ttp1.value == 1, "ttp1 should have one parameter.");
static_assert(ttp2.value == 2, "ttp2 should have two parameters.");
}
Both Clang and MSVC have problems with this, and both cause the static_assert
s to fail.
// Test case #2: What does and doesn't work.
#include <iostream>
#include <type_traits>
template<template<typename...> class VariadicParam,
typename... Ts>
struct S;
#ifdef TEST_TTP
// This section causes problems when enabled.
template<template<typename> class OneParam,
typename... Ts>
struct S<OneParam, Ts...> {};
template<template<typename, typename> class TwoParams,
typename... Ts>
struct S<TwoParams, Ts...> {};
#else // TEST_TTP
// This section compiles cleanly when enabled.
template<template<typename> class T,
typename TParam>
struct S<T, TParam> : T<TParam> {};
template<template<typename, typename> class T,
typename TParam1,
typename TParam2>
struct S<T, TParam1, TParam2> : T<TParam1, TParam2> {};
#endif // TEST_TTP
int main() {
S<std::is_arithmetic, int> s_one;
S<std::is_same, int, int> s_two;
}
This causes the same issue if TEST_TTP
is defined, but compiles cleanly if it isn't. It specifically illustrates that Clang and MSVC don't consider the number of parameters a template template parameter takes as grounds for partial specialisation, but GCC does, and was used as a basis for the MCVE.
Tested with Coliru GCC and Clang, Visual C++ online compiler, and TutorialsPoint online compiler. Links provided with Coliru and Rextester.
TEST_TTP
), do you believe it would be able to stand on its own? It would look like this (forgive the lack of Markdown formatting, I don't have a pastebin pro account).