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
7
votes
1 answer

Anonymous temporaries and class template argument deduction - gcc vs clang

Consider the following code snippet: template struct foo { foo(T) { } }; int main() { foo{0}; } g++ 7 happily creates a temporary object of type foo, deducing T = int. clang++ 5 and 6 refuse to compile the code: error:…
7
votes
2 answers

Template T Class Array

I have a (what should be simple) assignment in my C++ class. Here is the assignment: Create a class template that contains two private data members: T * array and int size. The class uses a constructor to allocate the array based on the size…
B. N.
  • 73
  • 1
  • 1
  • 5
6
votes
1 answer

enable_if works with gcc but not with clang and msvc

I learnt about the SFINAE principle and its various uses. Then I wrote the following program that compiles with gcc but not with msvc and clang. Live demo. #include #include template class Container { …
6
votes
3 answers

std::conditional - Invalid parameter type ‘void’ even when testing for 'void'

The following compiles on Visual Studio: template struct Test { using FunctionPointerType = std::conditional_t< std::is_same_v , ReturnType(*)() ,…
Wad
  • 1,454
  • 1
  • 16
  • 33
6
votes
2 answers

Can an injected class name be used as a type name in a friend declaration?

Consider this code: template class Singleton { }; class Logger : public Singleton { friend class Singleton; }; It compiles in gcc and clang, but is it valid? [temp.local].1 says: When it is used with a…
6
votes
1 answer

How to avoid this kind of code repetition?

In order to avoid code repetition, I need to do something like this (in my real code I have much more complex types similar to T1 and T2): template struct A {}; template struct B {}; template
Nick
  • 9,962
  • 4
  • 42
  • 80
6
votes
1 answer

How to check if a template parameter is a struct/class?

For a small example like this, I want to only accept T if T is a struct/class and reject builtin types like 'int', 'char', 'bool' etc. template struct MyStruct { T t; };
A. K.
  • 34,395
  • 15
  • 52
  • 89
6
votes
6 answers

Template class inside class template in c++

noob here still experimenting with templates. Trying to write a message processing class template template class MessageProcessor { //constructor, destructor defined //Code using t_ and other functions foo( void ) { //More code…
user106740
  • 107
  • 1
  • 1
  • 8
5
votes
2 answers

Clang accepts out of class destructor definition while gcc does not

I was writing an out-of-class destructor definition for a class template when I noticed that the program compiles with clang with c++17 and c++20 and also with gcc with c++17 but rejected with gcc c++20. Demo. template struct C { …
Jason
  • 36,170
  • 5
  • 26
  • 60
5
votes
2 answers

class template fails to compile when named lambda is used as template class argument or constructor argument

I'm currently experimenting with class template programming and I came across this weird behavior that I cant understand when passing a named lambda as its argument. Could somebody explain why (1) & (2) below does not work? template
diaz
  • 51
  • 1
5
votes
2 answers

Class template, which is independent of the permutations of its arguments

Consider a class template and auxiliary enum classes defined as follows: enum class Color {Red, Green, Blue} enum class ShowAxes {False, True} enum class ShowLabels {False, True} template< Color, ShowAxes, ShowLabels > class A {......}; The…
5
votes
1 answer

Execute code once for each C++ class template instance

Ok, this is a complicated one. I have a C++ class template that is instanciated many times. For each of these instances I need to execute a function that register some operators. This needs to be done only once per template instance before the first…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
5
votes
1 answer

Class template in PHP like in C++

Is is possible to create class template in PHP as in C++? PHP probably does not have a similar language structure (like template key words in C++), but maybe there is some clever trick to achieve similar functionality? I have a Point class that I…
user3106462
  • 189
  • 5
  • 12
5
votes
3 answers

Using static functions of a base class without specifying parameters to avoid ambiguity

Some of my base classes get tons of parameters. Now I want to specify which static function to use: template struct SBase { static void func() { } }; struct A : public SBase { }; struct B : public A, public…
hpohl
  • 315
  • 2
  • 11
4
votes
3 answers

The Immediate Window

We use fluentmigrator and it wants a long for the migration number. Normally I can just open the immedetiate window and type System.DateTime.Now.ToString("yyyMMddhhmmss"); But sometimes it will say: The expression cannot be evaluated while in…
Steve
  • 25,806
  • 2
  • 33
  • 43
1
2
3
16 17