template-function Questions

4

Solved

For example, we have a function like that: template <typename TYPE> void construct_and_destruct(TYPE & object) { //... } We cant call constructor and destructor like object.Type() and...

4

Solved

I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; templ...
Darrel asked 25/5, 2015 at 13:43

4

Solved

What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail...

2

Solved

If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl...

1

Solved

The following very simple code won't compile #include <vector> #include <string> namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const F...

1

Solved

First of all, This is not a duplicate question, because 1) this is a linker problem, compiler is passed successfully because I have explicitly instantiated. 2) It's not about template class, but te...

1

In the code below, why do the two ways of invoking fun: fun(num) and fun<const int>(num), give a different result when compiling? #include <iostream> using namespace std; template<...

1

Solved

I wonder what is the difference between this code that works : #include <type_traits> #include <iostream> template<typename T> using is_ref = std::enable_if_t<std::is_referen...
Egidius asked 20/5, 2018 at 23:41

3

As proper use of std::swap is: using std::swap; swap(a,b); It is a bit verbose but it makes sure that if a,b have better swap defined it gets picked. So now my question is why std::swap is not...

1

Solved

Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so...
Saunders asked 4/8, 2017 at 12:3

2

Solved

Ok, so i have this template class, which is kinda like one-way list. template <typename T> List and it have this inside function print public: void Print(); which, as you can guess, pri...
Oarlock asked 21/11, 2016 at 14:3

3

Solved

The answer to this question picks apart a function type using a class template: template <typename T> struct function_args {}; template <typename R, typename... Args> struct function_...

2

Solved

I have the following template method: struct MyStruct { // ... template<typename T> void readField(std::istream& in, T& data) { read(in, data); data = ntohl(data); } }; templ...
Sinecure asked 26/1, 2016 at 15:21

2

Solved

For a university exercise, I have been asked to write a template function "print();", which takes two arguments, 1: an array of a generic type, and 2: an int specifying the size of the array. The f...
Eolithic asked 20/10, 2015 at 11:5

6

Solved

After years of coding in c++, today I was asked a simple question, but indeed I couldn't find its answer and so here I am guys. Besides wondering why this error is happening, I want to know how I ...
Convertible asked 2/10, 2015 at 19:30

3

Solved

Note: the example provided in this question is not production code and has no sense at all. It is just there to illustrate my problem. I was testing the possibilities of decltype, especially if it...
Gnaw asked 21/9, 2015 at 2:52

3

Solved

I'm trying to specialize std::begin for a custom container. I'm doing this because I want to use range-based for with the container. This is what I have: class stackiterator { … }; class stack { …...

2

Solved

Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example, #include <iostream> #include <string> using std::cout; us...

3

In the following, GCC confuses template struct name with template member function name of class A, while Clang compiles fine (live example): template<typename T> struct name {}; struct A { ...
Skyway asked 11/3, 2014 at 0:23

1

Solved

The following code compiles in Visual C++ 2013 but not under G++ 4.8.2: template<class T> int MyFunc(T& t) { return static_cast<int>(CCodes::blah); } template<> int MyFunc(...
Garbe asked 23/7, 2014 at 6:46

1

Solved

I want to write a function which executes the method of some templated class, but should also compile fine if the class doesn't have it. In that case, it should just not call the function. struct ...
Ananias asked 1/5, 2014 at 19:41

3

Solved

I implemented a generic event emitter class which allows code to register callbacks, and emit events with arguments. I used Boost.Any type erasure to store the callbacks so they can have arbitrary ...
Radmilla asked 5/2, 2014 at 19:33

2

In the following, struct Y overloads X's member function f. Both overloads are template functions, but take different arguments (typename and int), to be explicitly specified: struct X { template...
Melaniemelanin asked 17/9, 2013 at 23:25

6

I am not quite proficient with templates. How do I write the a template function called get that chooses the array it gets from based on the template type? See the example below: struct Foo { int...
Pohl asked 23/6, 2011 at 2:17

4

Solved

Do I need inline template functions if they are included in several cpp files? Thanks. template<bool> inline QString GetText(); template<> inline QString GetText<true>() {return ...
Runner asked 16/7, 2013 at 2:36

© 2022 - 2024 — McMap. All rights reserved.