Questions tagged [template-classes]

184 questions
3
votes
2 answers

How to use lambda as STL Compare method in a template class?

I'm trying to implement a priority_queue which holds A objects and use a custom Compare method/type. According to the reference example, this is my code: template class A{ T value; A(T _value):value(_value){} }; template
Emadpres
  • 3,466
  • 2
  • 29
  • 44
3
votes
2 answers

Overridden << operator not recognized

I'm trying to override the << operator but it seems that the compiler doesn't recognize my implementation and instead tries to interpret it as a bit shift. I've already tried to play around with the parameter types (const T&, T&, T, const T) to no…
Anna P.
  • 33
  • 3
3
votes
1 answer

function as template parameter : if(T receive 2 param)T(a,b); else T(a);

How to make template class Collection receive a function T - that can either has signature T(K) or T(K,int) - as template argument, then conditionally compile base on the signature of the function? Here is the existing code that can receive 1…
javaLover
  • 6,347
  • 2
  • 22
  • 67
3
votes
2 answers

conversion between 2 types with different const qualifiers

This is a short example of the code I want to use: template class B { public : bool func1(const T& t) { // do something } }; class A { B b; public: void func2(const int* a) { b.func1(a); …
Adam
  • 464
  • 6
  • 16
3
votes
3 answers

Definition of different States of Streams (C++)

I know that ios_base has a declaration of states for streams like ios_base::goodbit(error state) ios_base::ate(file open mode state) and many more. What I'm interested in knowing is the definition of these member functions of ios_base Are they a…
Fiju
  • 416
  • 1
  • 5
  • 11
3
votes
1 answer

C++ Errors trying to overload the / operator in a templated class

I'm trying to overload the '+', '-', and '/' operators for a templated class i created. the + and - operators work perfectly, but the / operator overload is giving me errors. I'm using Visual Studio 2013 //Set.h #pragma once #include…
uktuk
  • 33
  • 2
3
votes
2 answers

Disable a function by throwing error at compile-time with template class using traits

I have a class, let's call it Foo with several methods: template class Foo { public: Foo() { /* ... */ } bool do_something() { /* ... */ } // This method should be callable only if: //…
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
3
votes
3 answers

Template class and possible performance issues

If I use a template class to create 30 different definitions. My question is that will it compile into 30 actual classes in binary (binary size = sizeof(template_class) x 30), although their actual code are very similar or even exactly the same ? …
Bryan Fok
  • 3,277
  • 2
  • 31
  • 59
3
votes
3 answers

Making this template class into a generic class

So I am a college student just looking for a little help and understanding, I have a professor that does not allow us to use java pre-written classes such as ArrayList, so I am trying to figure out how to modify my current encapsulated array class…
2
votes
1 answer

Member method calls virtual method with same name but different signature

I have the following Header/Source files: // foo.h #ifndef __FOO_H #define __FOO_H #include #include template class FooBase { public: std::map a; FooBase(std::map a_) : a(a_) {}; T…
flonk
  • 3,726
  • 3
  • 24
  • 37
2
votes
1 answer

syntax for starting a thread in a template class

I create an object of type T in a template class. template class worker { public: worker() { obj = new T(app_cfg);} ~worker() { if (obj) { delete obj; obj = nullptr; }} void start(); private: T * obj = nullptr; …
pipetka
  • 43
  • 3
2
votes
1 answer

Class Template non-type member access in sub-class

I have a Matrix class template which looks like this: using matrix_size_t = unsigned int; using matrix_data_t = double; template class Matrix { protected: matrix_data_t m_matrix[row][col]; //more code…
2
votes
3 answers

How can I handle circular #include calls in a template class header?

Related to Error "Unterminated conditional directive" in cross-referencing headers I have a Serializable template class: serializable.h #pragma once #ifndef SERIALIZABLE_H #define SERIALIZABLE_H #include "Logger.h" #include #include…
Kevin Rak
  • 336
  • 2
  • 14
2
votes
4 answers

Simplify large number of template specializations

So I have a tremendous number of template specializations of this template: template // Same struct foo { // Same using type_name = T; // Same foo(const int base) : _base(base) {} // May take other parameters void func(const…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

How to avoid include files spreading due to template classes

Due to the implementation of a template class in a header file, I have to include the other includes used in the implementation of the class in the header file. Because of that, each time I include my template class that bring all other the…
Moia
  • 2,216
  • 1
  • 12
  • 34
1
2
3
12 13