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

No viable constructor or deduction guide for deduction of template arguments of 'packaged_task'

The following code compiles on Windows under MSVC 2022 in a C++17 mode, but fails to compile on Linux with both GCC 11.3 and Clang 16.0 compilers. #include #include #include #include using namespace…
bobeff
  • 3,543
  • 3
  • 34
  • 62
0
votes
1 answer

How can I avoid writing every permutation of L and R value references for a function which does not allow universal references?

This example code below is designed to express the need to achieve 2 goals on multiple instances of other code: Maintain full type specification in template function bar. Avoid writing all permutations of l-value and r-value references for bar. As…
0
votes
0 answers

Calculator in C++ using class template to automatically call the datatype that the user enters

`I want to make a calculator in C++ for addition, subtraction, multiplication and division using class template and in the main function, if the user entered integer it should call the class template with the integer type, if user enters a float…
Dhruv
  • 1
  • 1
0
votes
1 answer

How to restrict a class template parameter to a certain subclass?

This is what I am trying: C is a template parameter that is either SomeClass or SomeDerivedClass: class SomeClass { protected: int ProtectedBaseClassMember; virtual void SomeFunctionFromBaseClass(); }; class SomeDerivedClass : public SomeClass…
ruben.moor
  • 1,876
  • 15
  • 27
0
votes
0 answers

Class templates changing inheritance behavior

I am trying to create a base class in C++ while using a class template. The class only contains a single protected data member (at least for ease of explaining). I am then trying to access said data member from a derived class. This works fine when…
Sjakk
  • 1
0
votes
1 answer

Passing template parameter pack to type_traits and std::enable_if

I am trying to create a class holding std::variant with a member function that would only accept types held by the nested variant object. That function works basically the same way as variant's operator=. However, the question is - how do I use…
J_S
  • 2,985
  • 3
  • 15
  • 38
0
votes
2 answers

How to create a friend function for template base class with constexpr

I want to create a overloaded operator<< for a template base class, that calls the toString function for the child class. The issue is that the toString function of the child class is constexpr, which mean I cannot make it a virtual function of the…
auzn
  • 613
  • 3
  • 14
0
votes
3 answers

How to initialize constexpr static class members per class instantiation basis?

Basically, I want to allow the clients of the class Foo to define its static constexpr member variables using arbitrary values based on the template type argument they pass to it when instantiating Foo. Here is an MRE: #include #include…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
1 answer

Global int alternative

i have an int linked list, and a function called filter which receives a list and a condition function. The filter function goes through the nodes in the list and if the condition is true, it adds it to a new list. I've created this so that it…
0
votes
3 answers

What is the need for having two different syntaxes for specializing data member of a class template

I was writing an example involving specialization of class template where I noticed that the standard allows two different syntax for specialization of a data member as shown below: template struct C { static int x; }; template<>…
0
votes
1 answer

Can we have an out of class definition for a class template that is a member of a class template

I know that the following snippet is valid: template struct Custom { template void func(); }; //valid template template void Custom::func() { } But can we do the same for a class…
Jason
  • 36,170
  • 5
  • 26
  • 60
0
votes
1 answer

can't find overloaded method from inherited class template

This is the first time I am using class templates so please don't be to harsh if I made a simply mistake. I have a class template class A. It has a method init() that is pure virtual and therefore will be implemented separately in every…
Tobxon
  • 35
  • 4
0
votes
1 answer

Having different specialization of a class template and the specialization definitions have functions with other specializations in its signature

So I have a class template for example in Template.h template class Something { public: static_assert(std::is_floating_point::value, "Floating Point only"); }; and I separated the float and double specialization into…
Moaz Cook
  • 3
  • 4
0
votes
3 answers

Is it possible to use variadic template parameters to initialise multi dimensional containers?

I am trying to use variadic template and constructor arguments to initialise values of a multi dimensional arrays inside a custom class ArrayND. Up until this point I've been successful initialising arbitrary dimensional Array instances with multi…
Iron Attorney
  • 1,003
  • 1
  • 9
  • 22
0
votes
0 answers

class template error C2510: 'T': left of '::' must be a class/struct/union

Hello I'm currently analyzing a source code below which works well in Linux (MinGW) but doesn't work in MSVC, visual studio in Windows. This code is following C++ 03 rule, not C++ 11. template class CTestClass { public: static const…