-6

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 and TwoParam are distinct from TTP in the primary template.
  • Clang fails to recognise that OneParam specialises TTP, causing it to emit two errors (the first being that the partial specialisation doesn't specialise any template parameters, and the second being that OneParam conflicts with the previously-declared template template parameter). It then emits similar errors for TwoParam (the first is identical, while the second says that the template template parameter has too many parameters), and an error for each instantiation of SS (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 of SS, 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_asserts 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.

16
  • 8
    Everything relevant to the question should be included in the question itself.
    – user4639281
    Commented Dec 8, 2016 at 19:25
  • 12
    Likewise, you should do what you can to keep irrelevant information out of your question. That question is an extremely low quality question in that it obfuscates the actually relevant information with tons of irrelevant information, making it much harder for people to find what matters.
    – Servy
    Commented Dec 8, 2016 at 19:31
  • 5
    tldr, but if the details aren't critical... why are they in the question or referenced at all.
    – Kevin B
    Commented Dec 8, 2016 at 19:44
  • Your question should be as short as possible but no shorter. Commented Dec 8, 2016 at 20:48
  • @Servy Okay, so just strip all of the "context" information out altogether, then? Commented Dec 8, 2016 at 23:33
  • @TinyGiant I considered them relevant because they show how each compiler treats the example code, but not critical because they don't suggest proper syntax for this, and I'm honestly not sure whether it's legal C++ in the first place. Commented Dec 8, 2016 at 23:38
  • @KevinB I had them there because they demonstrate how each compiler handles the issue, providing useful insight IMO. Commented Dec 8, 2016 at 23:41
  • Are those details required to answer the question? It can help show your intent, but too much information detracts from the question itself, no matter how much it helps to clarify your intent. If there's just too much to read, no one is going to read it.
    – user4639281
    Commented Dec 8, 2016 at 23:48
  • @TinyGiant I believe only the first segment of my intended edit, before the "Context" section, is strictly necessary (the code provided at the start allows the issue to be replicated, while also demonstrating an alternative that works properly with each compiler), with the rest there to provide additional information. I'm not 100% certain at the moment, though. Commented Dec 8, 2016 at 23:59
  • @TinyGiant Okay, if I strip out the "context" section, and leave only the segment from "Consider the following code" to the bolded question, while also adding the link "Demonstrated live on Coliru." (so that users can quickly verify it; the link contains a direct copy of the code, compiled with both GCC and Clang, with and without 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). Commented Dec 9, 2016 at 0:56
  • @Justin that looks like a much better question
    – user4639281
    Commented Dec 9, 2016 at 1:25
  • @TinyGiant ...And edited. Thanks for your feedback. Commented Dec 9, 2016 at 2:20
  • 1
    @Kevin B: Because, for all we know, those details might actually be critical. Better left in than left out, I guess.
    – BoltClock Mod
    Commented Dec 9, 2016 at 5:11
  • @BoltClock And yet including pages and pages of irrelevant information can very often dramatically reduce clarity of the question and make it harder to see what's actually being asked. It draws attention away from the important information and distracts readers into tangentially related topics (or results in readers incorrectly assuming that the related information is what they're actually asking about, rather than just being tangential). It also just results in lots of people not bothering to sort through the mess and ignoring the question entirely.
    – Servy
    Commented Dec 9, 2016 at 14:27
  • @Servy: Yes, askers eventually have to learn to include just the necessary information and nothing more, or less. It's on them to figure out what's relevant to the question at hand and what isn't.
    – BoltClock Mod
    Commented Dec 9, 2016 at 16:21

2 Answers 2

7

Your question should be as short as necessary but no shorter. It should be self-contained. Links for reference are okay, but should not be critical to understanding your question. You should include all details that illustrate the problem which you are asking about but not include any irrelevant details. Ideally, you should have a complete code example that anyone can copy and paste, compile themselves, and get the exact same result that you do.

When you provide a lot of code you increase the chance of introducing an error which hides the real issue. Also, someone with a potential answer can get hung up on compiler errors such add undefined variable names which have nothing to do with your question. Asking for clarification for such things wastes both your time and ours and delays any answer which allows you to move on with your work.

I assume that you ask questions on Stack Overflow because you want help with problems you are encountering in your own programming projects. You should keep this in mind as you are writing your question. In order to achieve your goal as quickly as possible, you should write your questions in such a way as to attract people with the knowledge you need and to convince them to share that knowledge. If your question is too long, the people you want help from will be less willing to wade through all the details. If it is too short, they will be unwilling to take a lot of time asking for clarifications.

Note: My first sentence is a play on a quote attributed to Albert Einstein: "Make things as simple as possible but no simpler."

14
  • 2
    "Your question should be as short as possible but no shorter" is ambiguous. It may be better to say "Your question should contain all details necessary to answer the question, but should contain as little irrelevant information as possible."
    – user4639281
    Commented Dec 8, 2016 at 20:55
  • 3
    @TinyGiant Yes, I agree there is some ambiguity. However, it is more catchy than your longer-winded version. I'm actually paraphrasing a quote from Albert Einstein. I also expand to explain the issue. Commented Dec 8, 2016 at 20:58
  • 2
    @TinyGiant simple as possible but no simpler - Albert Einstein Commented Dec 8, 2016 at 20:59
  • Both my original question and the edit I was planning to make contain an MCVE that demonstrates the issue (the one with TEST_TTP). I guess I wasn't as clear about that as I thought I was, though. Commented Dec 8, 2016 at 23:49
  • @Code-Apprentice Thanks for your feedback. I wasn't sure because those specific error messages are useful, but not critical to the issue (they indicate how each compiler handles the code, but don't suggest proper syntax or indicate whether it's legal C++). I ended up stripping them out of the intended edit, along with the entire "context" section, since the MCVE demonstrates the issue, and making a note to refer to edit history if a record of the tests I performed, and the original example, are desired (since they're strictly unnecessary for the question itself), majorly shortening the post. Commented Dec 9, 2016 at 2:38
  • @Code-Apprentice my issue with the phrasing is that you can ask a question with literally a single statement. That is the shortest possible actual question. Questions like that are generally not good questions, if even clear enough to answer. We need to focus first on making sure that questions have enough detail to be answerable and to determine a reasonable scope, then worry about making sure that they don't include an excess of irrelevant material so that people will actually want to answer.
    – user4639281
    Commented Dec 9, 2016 at 3:03
  • @TinyGiant your example of a question with only one sentence most likely violates the "but no shorter" clause of my maxim. We seem to both agree that most of the time such a question is too short. At the same time, several hundred lines of code is just as bad. Not many people will wade through all of that code to find the few lines which are germane to the point at hand. Commented Dec 9, 2016 at 5:05
  • @Code-Apprentice. Please re-append the link. Takes me to a blank page on Firefox. #CuriouserAndCuriouser :) Commented Aug 3, 2017 at 4:06
  • @George I assume you mean the link in my comment above, correct? I, too, noticed that it no longer works. Commented Aug 3, 2017 at 4:24
  • @Code-Apprentice : Yes, the Einstein one. Commented Aug 3, 2017 at 4:31
  • @George Added a new link to my answer. p.s. The link is just a google search Commented Aug 3, 2017 at 6:06
  • @Code-Apprentice Good initiative. The search query may be edited to have the language as english instead of espanol :P (hl=en instead of hl=es) . Whatsay you? Commented Aug 3, 2017 at 6:09
  • @George Or no language specified...I am learning Spanish and even though I switch my settings back to English, I still get Spanish on many sites. Commented Aug 3, 2017 at 6:12
  • @Code-Apprentice Even better. :) Cheers, mate. Commented Aug 3, 2017 at 6:18
4

It's very important to keep posts reasonably scoped.

I cannot stress that enough. SO is a Q&A site. A question should be one question, not your life story.

Reread Minimal, complete and Verifiable Example. A question should include everything directly relevant to the core problem and nothing else. It's your job to isolate those parts before asking. Dumping that task on us instead is not okay1 and will be dealt with accordingly.

(As a one that has seen my share of these "information dump"... "questions", so to say (including this one), I cannot help but express my irritation with them.)

1It is tolerated if it's not too much work but that doesn't mean it's okay as the general approach. Also, "tolerated" means "the question does not get downvoted to oblivion and/or closed", it still affects the attitude towards it and, as a result, the aggregate response you get.

9
  • Good point. I thought the context, and specific tests that helped narrow down the particular issue, would be useful as long as an MCVE was also provided; in both my original question and the edit I was planning to make, this MCVE was the one with TEST_TTP, above the section with the bolded text. Looks like that wasn't as clear as I thought it was, though. My bad. Commented Dec 8, 2016 at 23:47
  • 1
    @Justin The acronym MCVE applies to all examples, not just the example demonstrating your problem.
    – user4639281
    Commented Dec 8, 2016 at 23:50
  • @TinyGiant I was not aware of that. That would be a problem with my question, then. Commented Dec 9, 2016 at 0:02
  • @ivan_pozdeev Thanks for the feedback. I tend to consider all of the related information to be necessary, or at the very least relevant and useful, hence the rather long question. As it stands now, the entire "context" section has been removed from the intended edit because it isn't strictly necessary (the MCVE demonstrates the issue), and I made a note to refer to the edit history if a record of the tests I performed, and the original example, are desired; it's much shorter now. Commented Dec 9, 2016 at 2:43
  • @JustinTime questions asking for help localizing the problem are considered off-topic specifically because they cannot fit into the Q&A format - and because they are generally non-reusable. Commented Dec 9, 2016 at 11:40
  • @ivan_pozdeev It's not actually about localising the problem, IMO; I know exactly where and what the issue is (and pointed out exactly what lines are responsible), and how to solve it; I'm just curious about whether what I was attempting is actually legal in C++ (and if it is, what the official syntax for it is), since I couldn't find a definitive answer on CPPReference or in the standard; the closest I found was a comment in [temp.arg.template/3], // ill-formed: a template parameter pack does not match a template parameter, used in a similar context. Commented Dec 11, 2016 at 20:45
  • (Specifically, the comment is about whether a template class with a variadic template, template <class ... Types> class C { /* ... */ };, can be used as a parameter for a template template parameter whose template parameter takes one parameter, template<template<class> class P> class X { /* ... */ };, as X<C> xc;. The comment implies that template<template<class> class P> and template<template<class...> class P> are distinct, which makes me suspect that it should be legal in some fashion.) Commented Dec 11, 2016 at 20:55
  • If it's not legal, then the question somewhat turns into a localisation question, as the follow-up asks whether GCC's support of it is a bug and/or a GCC extension. Commented Dec 11, 2016 at 20:55
  • ...I may not have specified that clearly enough in the question itself, though, and might need to reword it. Commented Dec 11, 2016 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .