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
4
votes
2 answers

How to avoid redefinition error in case of in-class definition of friend function template?

Consider this code: template class Base { template friend void f(void *ptr) { static_cast*>(ptr)->run(); } protected: virtual void run() = 0; }; class A : public Base { protected: …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4
votes
3 answers

How to create a type trait to avoid writing redundant specializations?

I have a class template with a primary template that is meant to work with any type argument T. However, due to some particular needs, I need to use template specialization like this: template class foo { private: T result; public: …
4
votes
1 answer

Enforce compile time maximum value to a integral constant expression

I have a template class like so: template class Array { public: static const unsigned int SIZE = size; // ... private: T data[SIZE]; }; Is there a way to add compile-time checks such that the size of an…
JoeVictor
  • 1,806
  • 1
  • 17
  • 38
4
votes
1 answer

Explicit member function template specialization bug in GCC vs Clang

I was trying to explicitly specialize a member function template when i noticed that one such case(given below) compiles fine in clang and msvc but not in gcc. Here is the link for the verification of the same: https://godbolt.org/z/15z4nT5Kx struct…
Jason
  • 36,170
  • 5
  • 26
  • 60
4
votes
2 answers

class template's constructor declaration doesn't compile for C++20 but compiles for C++17

I am learning about templates in C++. In particular, i saw here that we can have the following declaration for the constructor: template struct Rational { Rational(); }; But the above snippet fails to compile in C++2a and…
Jason
  • 36,170
  • 5
  • 26
  • 60
4
votes
2 answers

C++ - Deducing type from inherited constructor's arguments for class template

I have a class template that inherits the constructors of the base class template. (As for c++20) Is there a way to deduce the template arguments of the derived class from the constructor arguments of base? If I specify the type explicitly, that…
Newline
  • 769
  • 3
  • 12
4
votes
1 answer

Class template and friendship

I have this example on class templates and friends: template class Foo; template std::ostream& operator<<(std::ostream&, Foo const&); template std::istream& operator>>(std::istream&, Foo&); template…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
4
votes
1 answer

Type parameter behind the “function name” operator

what is the difference between the following two snippets? with for operator << template class Stack { ... friend std::ostream& operator<< (std::ostream&, Stack const&); }; without template class Stack…
Willi
  • 317
  • 1
  • 8
4
votes
3 answers

What is the advantage of std::enable_if over static_assert for class templates?

I was wondering about the advantage of std::enable_if over static_asserts to prevent template instantiation. This answer suggests, that std::enable_if allows SFINAE, which is a convincing argument in the case of function templates. However is this…
Reizo
  • 1,374
  • 12
  • 17
4
votes
1 answer

When are member functions of a templated class instantiated?

Consider the following example: template class Base { public: inline void fooBase () { T t; // The following error only occurs when class ABC is not defined at the end of the file: "error: t uses undefined class ABC" …
m7913d
  • 10,244
  • 7
  • 28
  • 56
4
votes
1 answer

C++17 class template deduction const-ness

I am attempting to use the new c++17 class template deduction and it all seems to work fine until I apply const. This is a small example of the trouble I'm facing: #include template struct X { T _data; X(void) =…
4
votes
2 answers

Why can't I use QList::size_type as I would std::string::size_type? (template parameter error)

While researching an unsigned vs. signed integer comparison warning when declaring an iterator in my for loop, I read this: Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_type when comparing…
Zimano
  • 1,870
  • 2
  • 23
  • 41
4
votes
1 answer

C++ template type deduction from arguments of a function pointer

I have a template which looks something like this: template< typename T, void (*f)( T& param )> class SomeAction { ... }; f is used inside SomeAction (actually f is a class member, but I don't think it's matter). Question is: can this be improved…
Leo
  • 131
  • 1
  • 9
4
votes
1 answer

Define friend function template of class template

I want to define a function template of a class template. The code looks like this. template struct test{ private: int value; template friend auto foo(test const t){ test r; r.value = t.value; …
4
votes
1 answer

Why SFINAE requires the 'Enable' class template parameter?

(this question is not related to C++11/C++14: the examples are compiled using C++03) enable_bool has a member ::type only when T is bool template struct enable_bool {}; template <> struct enable_bool< bool > { typedef bool type; }; In…
oHo
  • 51,447
  • 27
  • 165
  • 200
1 2
3
16 17