class-template Questions

3

Solved

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 con...
Lynnlynna asked 10/1, 2018 at 16:35

1

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 <iostream> #include <ty...
Faceless asked 25/11, 2022 at 13:8

3

Solved

The following compiles on Visual Studio: template<typename ArgType, typename ReturnType> struct Test { using FunctionPointerType = std::conditional_t< std::is_same_v<ArgType, void>...
Deft asked 25/5, 2022 at 16:20

6

Solved

If I am allowed to do the following: template <typename T = int> class Foo{ }; Why am I not allowed to do the following in main? Foo me; But I must specify the following: Foo<int&gt...
Trisyllable asked 12/3, 2013 at 22:52

2

Solved

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...

2

Solved

Consider this code: template <typename T> class Singleton { }; class Logger : public Singleton<Logger> { friend class Singleton; }; It compiles in gcc and clang, but is it valid? [te...

4

Solved

According to https://en.cppreference.com/, std::vector<bool> has a class template specialization, while std::array<bool, N> does not. Which are the reasons why it is not provided?
Lawgiver asked 16/11, 2020 at 16:52

1

Solved

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 <class T1, class T2> struct A {}; templat...
Glyph asked 3/8, 2020 at 20:1

1

Solved

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<typename T> struct MyStruct { T t; };
Allegraallegretto asked 31/7, 2020 at 20:10

3

Solved

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 templ...
Sleight asked 7/7, 2020 at 7:35

1

Solved

How are designated initializers (C++20) supposed to work with CTAD? This code works fine in gcc9.2, but fails with clang8 template <typename int_t=int, typename float_t=float> struct my_pai...

2

Solved

In the example below, we use the C++17 feature "Class template argument deduction" to deduce that val is of type Base<int, double, bool>: template<class T, class U, class V> struct Bas...
Pasquale asked 19/8, 2019 at 20:33

2

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) ...

3

Solved

In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still ...
Cleasta asked 14/5, 2019 at 12:43

3

Solved

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 &...

3

Solved

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 <typename T=int> class tt { pu...
Keppel asked 15/4, 2013 at 12:5

2

Solved

I have the following code: template <class T> class lit { public: lit(T l) : val(l) {} T val; }; template <class T> class cat { public: cat(lit<T> const& a, lit<T> ...

3

Solved

I'd like to do this: template <typename T> struct S { ... static double something_relevant = 1.5; }; but I can't since something_relevant is not of integral type. It doesn't depend on T,...
Selden asked 12/7, 2010 at 15:45

2

template<class Key1, class Key2, class Type> class DualMultimapCache { public: std::list<std::reference_wrapper<Type>> get(Key1 const & key); std::list<std::reference_...
Danialah asked 11/12, 2017 at 14:26

2

Solved

I know that for the following function template <typename T> void do_something(T&& arg); the function parameter is a forwarding reference. But in the following case, is it still a fo...
Scirrhous asked 14/9, 2017 at 8:8

2

Solved

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, p051...

1

Solved

Assume that I have the following bunch of files: Generic.h: Complicated template class #pragma once template<typename K, typename V, template<typename Key, typename Value, typename ...>...
Bromeosin asked 7/7, 2016 at 12:3

1

I want to define a function template of a class template. The code looks like this. template<int M> struct test{ private: int value; template<int N = 2 * M> friend auto foo(test co...

2

Solved

The following program #include <algorithm> #include <utility> #include <memory> namespace my_namespace { template<class T> void swap(T& a, T& b) { T tmp = std::...

2

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 ...
Twentyfour asked 6/9, 2015 at 10:37

© 2022 - 2024 — McMap. All rights reserved.