Questions tagged [default-template-argument]

16 questions
3
votes
1 answer

Non-defaulted template params after a defaulted param: class vs function

From this page on cppreference about default template arguments: If the default is specified for a template parameter of a primary class template, [...] each subsequent template parameter must have a default argument, except the very last one may…
paolo
  • 2,345
  • 1
  • 3
  • 17
3
votes
3 answers

Defaulted Template in Template Function Requires Empty Angle Brackets <>

gcc 11.2 can't seem to compile this: template struct Test {}; template void foo(T& bar) {} int main() { Test t; foo(t); } but has no problem with template struct Test {}; template…
3
votes
1 answer

How to provide a default parameter argument when the type of that parameter is a template type?

template class Pair { public: Pair(const K& key, const V& value = initial) { // what should "initial" be here? // ... } } For example if I use the class like this: int main() { Pair
3
votes
3 answers

Special overload for pointer types

Consider the following snippet: template struct remove_pointer { }; template struct remove_pointer { typedef T type; }; template T clone(const T& v) { return v; } template
user3612643
  • 5,096
  • 7
  • 34
  • 55
2
votes
1 answer

CTAD fails for templated base class

Consider the following simple construct. I derive a class type EntityView from Entity which allows me to specify an allocator if I want to, and if I don't it should fall back to the defaulted template parameter. Yet the compiler complains that it…
glades
  • 3,778
  • 1
  • 12
  • 34
2
votes
1 answer

template specialization and default template parameters and sfinae

I have put together an example on creating a base template with a number of specializations. #include template struct foo { static void apply() { std::cout << "a: " << __FUNCTION__ <<…
1
vote
1 answer

default template argument and parameter pack

In a classic recursive template specialization I need one type multiple times in the class definition, e.g. for inheriting and using statement, maybe on other places also. Example: template < typename ... T > class C; template < typename FIRST,…
Klaus
  • 24,205
  • 7
  • 58
  • 113
1
vote
1 answer

Declaring a default templated function

Suppose I have the following: // Foo.h struct Foo {}; // Bar.h struct Bar { template ::value>::type> void foobar(); }; // Bar.hpp // ...? void Bar::foobar() {} How do…
Yves Calaci
  • 1,019
  • 1
  • 11
  • 37
0
votes
3 answers

multiple default template argument cases that depend on a template argument respectively

I would like to define two particular default cases for a template class A. Is something similar possible?: template class A{ // ... }; struct X; // if necessary, I can also define X right here. template<> A =…
kaisong
  • 65
  • 6
0
votes
2 answers

How to make C++ template type to default to previous template type

Is there any way, to default second template type to the type of the first template argument type, to remove the need of specifying the template types when not needed? So, I have a method, that pops values from byte array: template
0
votes
1 answer

Syntax for define a template that takes a template

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>. So, I went for it: #include template class…
0
votes
1 answer

CTAD doesn't work with defaulted template arguments?

Compare the following case when I have a class object that takes a vector. The non-deduced parameter T can be substituted fine with the default template argument: #include template struct container { …
0
votes
1 answer

C++17 pointer to tempalte class with class template default argument

I am experimenting with C++17 class template default argument and was wondering if anyone could explain: If I have the following: template class MyClass { //class goes here }; And then try to use it as: MyClass *…
0
votes
1 answer

Partial template specialization: template argument X is invalid

I want to partially specialize a class for when the first template parameter is not a POD-type. Here's what I came up with: godbolt #include #include template > struct A { void…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

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 class X {}; template class X> class Y {}; int main() { …
1
2