Questions tagged [class-template]

Anything related to C++ class templates, i.e. classes whose definition depends on some parameter according to templates definition rules.

Anything related to C++ class templates, i.e. classes whose definition depends on some parameter according to templates definition rules.

See:

243 questions
155
votes
1 answer

Template function inside template class

I have this code: template class MyClass { public: template void foo() { U a; a.invoke(); } }; I want it in this form: template class MyClass { public: template void…
Michael
  • 2,356
  • 3
  • 21
  • 24
87
votes
3 answers

Will consteval functions allow template parameters dependent on function arguments?

In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at…
Annyo
  • 1,387
  • 9
  • 20
27
votes
4 answers

No class template specialization for array of bool?

According to https://en.cppreference.com/, std::vector has a class template specialization, while std::array does not. Which are the reasons why it is not provided?
23
votes
2 answers

Why is this call to swap() ambiguous?

The following program #include #include #include namespace my_namespace { template void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template
20
votes
3 answers

Template class with conditional typenames

I would like to have a template class (e.g. float/double type), but I am using Nvidia CUDA and OptiX and have multiple other types (e.g. float2, double2, float3,...) that depend on the chosen template type. Something like this: #include…
SemtexB
  • 660
  • 6
  • 21
14
votes
1 answer

C++20 designated initializers with templated types

How are designated initializers (C++20) supposed to work with CTAD? This code works fine in gcc9.2, but fails with clang8 template struct my_pair { int_t first; float_t…
13
votes
2 answers

Nested template argument deduction for class templates not working

In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates (p0091r3, p0512r0) #include #include #include…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
12
votes
2 answers

Partial class template argument deduction in C++17

In the example below, we use the C++17 feature "Class template argument deduction" to deduce that val is of type Base: template struct Base { Base(T, U) { }; Base(T, U, V) { }; Base(V) {…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
10
votes
2 answers

g++ c++17 class template argument deduction not working in a very specific case

I have the following code: template class lit { public: lit(T l) : val(l) {} T val; }; template class cat { public: cat(lit const& a, lit const& b) : a(a), b(b) {} lit const& a; lit const&…
Baruch
  • 20,590
  • 28
  • 126
  • 201
10
votes
1 answer

Correct forward declaration of fully specialized template classes

Assume that I have the following bunch of files: Generic.h: Complicated template class #pragma once template typename C> struct GenericMap { C
Aleph0
  • 5,816
  • 4
  • 29
  • 80
10
votes
2 answers

Should the following program compile according to standard?

After my discovery of incosistency between MSVC and GCC (probably clang too) in compilation and linking of the same code, I've become curious should this program actually compile and link and thus it's bug in MSVC (which reports a linker error) or…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
10
votes
3 answers

Avoid angle brackets in default template

If I have a template class with a default template type, I have to write the template angle brackets. Is it somehow possible to avoid this? Example: template class tt { public: T get() { return 5; } }; ... tt<> t; // how to…
user1810087
  • 5,146
  • 1
  • 41
  • 76
9
votes
2 answers

Has CRTP no compile time check?

I was trying to implement static polymorphism using the Curiously Recurring Template Pattern, when I noticed that static_cast<>, which usually checks at compile time if a type is actually convertible to another, missed a typo in the base class…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
7
votes
3 answers

Copy templated function argument in Eigen

I am writing a generic class that utilizes Eigen data types. I already have problems assigning constructor arguments to class member variables. A simplified version of my code would be: template class A { public: …
7
votes
2 answers

Template and overloads

template class DualMultimapCache { public: std::list> get(Key1 const & key); std::list> get(Key2 const & key); template
1
2 3
16 17