278 questions
2
votes
0
answers
95
views
Concept constraints for outer definitions of methods of a class with template template parameter
Usually I define my template classes as following (template<template<class> class T> is essential here):
// Foo.h
template<template<class> class T>
class Foo {
void foo();
...
3
votes
2
answers
148
views
Template template with container argument in C++17
To create a C++ class template that takes a standard container as a template argument I would do something like
#include <vector>
template <template<typename U,typename A=std::allocator&...
3
votes
0
answers
167
views
Why does C++26 still not allow passing a function template as a template parameter?
In C++26, the following code is legal (see p2841r7 and Herb Sutter's article):
template<typename<typename> concept C, template<typename> auto vt, typename T>
requires C<T>
auto ...
-1
votes
1
answer
108
views
How do I specify a default value for a template template parameter? [duplicate]
I have a function which takes a template template parameter, e.g.:
template <template <typename K, typename V> typename Map>
void foo();
I want to specify a default value for Map; namely, ...
2
votes
1
answer
75
views
variadic-value-template-template with value-types given in parent template is accepted by clang but rejected by gcc
I'm building trait-types (similar to std::type_identity) to represent template-templates of the different flavors. Mixed-type variadic value templates are a special case, and I can only think of doing ...
1
vote
0
answers
44
views
Template-template function overloading ambiguity error using gcc
I have this code perfectly working on clang
template <template <typename, typename> typename T>
struct CollectIntoAllocatedContainer
{
// ...
};
template <template <typename, ...
1
vote
2
answers
94
views
Template function member as template parameter
I want to call different template member functions with the same sequence of input types, in order to reduce boilerplate code, I tried to implement a dispatcher function, for_each_type,
struct A {
...
2
votes
1
answer
73
views
How do I use partial specializations as template template parameters?
I have a class template taking a template template parameter
template<template<class> typename T>
struct S {};
and a class template taking 2 template parameters
template<size_t i, ...
0
votes
1
answer
67
views
C++ TypeList with template types has index and type operations that don't work together
For a project I'm working on I have been trying to implement a typelist with template class.s as the possible types. The relevant parts of my implementation are below. It works well generally but it ...
0
votes
1
answer
92
views
How to work around MSVC not deducing template template argument
There is a very similar question, but I did not understand how I could apply the workarounds to my code. It was more about achieving generality across different container types: Visual C++ cannot ...
1
vote
0
answers
36
views
template template concepts to allow concepts to be templated on other concepts? [duplicate]
In C++20, is it possible for a concept to be templated on another concept?
I can do
template <typename R>
concept RangeOfFloats = std::ranges::range<R> && std::floating_point<...
1
vote
1
answer
102
views
An alternative for C++ not allowing templated type aliases?
Imagine a library has class templates A and B:
template <template <class> class T> class A;
template <class T, class U> class B;
Now, if a user of the library wants to pass B as an ...
0
votes
1
answer
99
views
Is it possible to write a C++ template function whose template arg works if-and-only-if it is template? [duplicate]
I'm trying to write a little template function that returns sequential integer id's for templates passed to it. The following works for many templates, but not for templates that take non-type ...
1
vote
2
answers
93
views
how do I "tighten up" arguments to a templatized function?
I have two related types (duck-typing), and another one that provides similar functionality with a different interface:
namespace a
{
template<typename T>
struct A final {};
using ...
1
vote
2
answers
69
views
Enforcing a common template type parameter among two template type parameters that are themselves templates
In C++, is there any way to ensure that two or more template type parameters are themselves template types with a common template type parameter?
Let's say that I have this:
struct ArbitraryType {};
...
4
votes
0
answers
111
views
Template template parameter with a concept - why does GCC reject it?
I'm writing a generic struct that is templated on another template. I want the inner template to only accept types that match a concept.
template<template<std::regular> typename T> struct ...
2
votes
3
answers
106
views
Deducing the size of an array when a template template parameter is used
A very simple method to find the number of elements in a template array is shown in the following discussion:
How does this function template deduce the size of an array?
I wanted to emulate the same ...
0
votes
1
answer
350
views
Template Template Parameters - Invalid Explicitly Specified Argument
I am just learning how to work with template template parameters and am having trouble invoking a function.
Compiler Error:
candidate template ignored: invalid explicitly-specified argument for ...
1
vote
2
answers
2k
views
C++: For loop index as constexpr / template parameter
Language & standard: C++17
What I hope to achieve:
I have created a global array, say int List[someconstant], which is constexpr constructed ; as such, I can pass its entries as template ...
4
votes
1
answer
351
views
Is an alias template considered the same template template parameter as the original template?
As I was playing around I stumbled over the following code snippet, which surprisingly doesn't match my expectations.
#include <tuple>
#include <type_traits>
template <class... Ts>
...
0
votes
1
answer
427
views
Syntax for define a template that takes a template [duplicate]
I wanted to create a type that holds a generic type type, that, itself, is a template with one argument. So, if my type is called C, it could be summarized like C<T<U>>.
So, I went for it:
...
4
votes
0
answers
85
views
Allow metafunction to take templates with non-type and type template arguments regardless
I created a is_specialization_of concept based on the type traits found here. The concept checks if a type is a specialization of a certain template. Those work well in all cases where I just have ...
5
votes
0
answers
65
views
Expansion of variadic type parameters of outer struct within member struct's template-template parameter
If you have a template struct then you can use T as the type of a value parameter of a member template:
template <typename T>
struct SMoo
{
template <T I, typename T2>
...
7
votes
1
answer
308
views
Is a template with reference non-type template parameter supposed to match a template template parameter with an auto non-type template parameter?
template<template<auto> class> struct A {};
template<int&> struct B {};
A<B> a;
int main() {}
All three compilers MSVC, GCC and Clang in their latest versions accept ...
1
vote
2
answers
96
views
c++ template template syntax: simplicity vs useability why not 'auto'
first code below compiled fine after sweating with the word 'class' five times
in one line, and definition in "main" of shelf<std::vector, int> top_shelf;
looks too fragmented to me, ...
0
votes
1
answer
126
views
static member definition outside class template template
I'm getting:
error: default argument for template parameter for class enclosing 'ticker<T, E, A>::garbage_element'
51 | E ticker<T,E,A> ::garbage_element;
| ^~~~~...
6
votes
3
answers
2k
views
Can C++ template template parameters accept templates that take non-type parameters?
I have a function like this to implement fmap for C++:
// Given a mapping F from T to U and a container of T, return a container of U
// whose elements are created by the mapping from the original ...
2
votes
1
answer
1k
views
C++ Template queue with template class
My template queue is below
template <typename T>
class LockingQueue
{
private:
std::queue<T> s_queue;
public:
void push(T const& value)
{}
T pop()
{}
};
And my ...
2
votes
1
answer
414
views
c++ - passing standard container as a template template parameter
So, I need to make a mixin class that would encapsulate children of some derived class. The derived class should inherit from the mixin while providing a container template as a template template ...
1
vote
2
answers
123
views
Cascade variadic template template parameters
How can I cascade variadic types? I.e.:
template <typename... T>
using Cascade = ???; // T1<T2<T3<...>>>
Example:
using Vector2D = Cascade<std::vector, std::vector, double&...
7
votes
1
answer
385
views
Exact rules for matching variadic template template parameters in partial template specialization
While creating this answer for another question I came around the following issue. Consider this program (godbolt):
#include <variant>
#include <iostream>
template <typename T>
...
3
votes
0
answers
158
views
Is there a way to typedef a template template argument similar to typedef a template argument
I'm wondering if there is or will be a way in C++ which allows to typedef a template template argument similar to typedef a template argument.
The only way i know of at the moment is using an alias ...
2
votes
0
answers
474
views
Deduce template parameter of class member from constructor of class
I have a class A which contains a templated member B whose exact type should be deduced from A's constructor. The way this is supposed to work is that, as shown in the below example, B can be ...
-2
votes
1
answer
126
views
C++20 Pass Familiar Template lambda as Class Template to Template Parameter of a Class
Consider passing a callable class template as a template parameter to some class. Three approaches as below but only functor one works.
The naive template function failed because it cannot serve as a ...
6
votes
0
answers
44
views
Try to remember template template argument passed into template for later re-use [duplicate]
I have a question that seems to be about template template arguments. Following code (life code on GodBolt.org) compiles with GCC up to 11.2, but it does not compiles with Clang++ up to 13.0.1.
The ...
2
votes
2
answers
413
views
Compile-time map on a type list
I am looking for an idiomatic way to apply a type-level transform for each element in a type list. So far I came up with the following, which works as expected:
namespace impl_
{
template <template ...
2
votes
0
answers
46
views
Template class inheritance from template types while passing itself [duplicate]
Can someone please explain what is going on in the code below:
template<class Thermo, template<class> class Type>
class thermo
:
public Thermo,
public Type<thermo<Thermo, ...
1
vote
2
answers
1k
views
Using a type without template arguments as a template argument
I have a class named Registry which correlates an ID with some data.
I would like to make it so the underlying structure which stores these pairs can be any std::mapish type, of which the user can ...
0
votes
0
answers
43
views
template-determined class as return-type of a template function, that uses template template parameters
I have std::vector<double> , and my type Array-like.
Also, there are a lot of numbers in std::vector<double> source, and I would like to make this code work in the way:
std::cout << ...
1
vote
1
answer
753
views
Issue with variadic template template parameter pack
Consider the following example:
template< class A, int B, class C>
struct Obj {
A a_obj;
static constexpr int b_value = B;
C c_obj;
};
template< template<class... > class ... ...
8
votes
1
answer
715
views
Why is the concept in template template argument not verified?
C++20 allows the program to specify concept for template template argument. For example,
#include <concepts>
template <typename T> concept Char = std::same_as<T, char>;
template <...
0
votes
1
answer
72
views
Compare templates itselves and not instantiated template-types
My goal is to be able to compare templates i.e. write something like this
template<typename T>
struct template_type;
template<template <typename...> typename TTmpl, typename ...Ts>
...
1
vote
2
answers
298
views
Why is there no variable template template parameter?
I'm planning to create a variable template that takes (variable) template-template parameter and one typename:
template <template <typename> auto MetaPredicate, typename T>
constexpr bool ...
5
votes
3
answers
1k
views
Get number of template parameters with template template function
I'm not sure if this is possible, but I would like to count the number of template arguments of any class like:
template <typename T>
class MyTemplateClass { ... };
template <typename T, ...
1
vote
1
answer
869
views
Template default argument
How do I specify a template class as a default value for a template typename? e.g. the following doesn't work.
template <typename A, typename B> class X {};
template <typename T=template <...
1
vote
1
answer
276
views
Is it possible to deduce template template parameter parameter (no typo :-) )
I have a class which implements data types for a huge project. I simplify it here to (actually it is a subclass passing typeID and subTypeID to iots parent class constructor):
template<typename T,...
3
votes
1
answer
145
views
What "conversion" of template template parameters is allowed in C++?
I'm trying to understand under what circumstances I can pass a type template as an argument for a template template parameter with a different signature. E.g., I would expect that the following might ...
5
votes
1
answer
2k
views
Visual C++ cannot deduce template template parameter
The following snippet of C++17 code compiles in GCC and CLang, but in Visual C++ it gives these errors:
<source>(14): error C2672: 'f': no matching overloaded function found
<source>(14): ...
2
votes
1
answer
536
views
Do C++20 concepts enable fixing template function as template argument problem?
If you try something relatively simple in C++20 it implodes with unhelpful error message spam.
int main() {
auto as = std::vector{1,3,24,};
auto bs = std::vector{1,4,10};
auto cs = std::...
2
votes
2
answers
457
views
How to get at a C++ Container<T>'s T if no Container::value_type is provided?
It's rather common that container templates contain a value_type typedef. This makes it easy to create other templated code, most recently concepts, which are able to extract the T if only having been ...