templates

Explicit Instantiation of Template

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 07:11:46
问题 This thread has been really helpful, but I still have questions about this process that don't appear to be answered. To what extent must I explicitly instantiate a template? For example, if in my definitions file I am using templates on every function, friend class, operator overload, etc, must I instantiate each in a template instantiation file (the current method that I am using)? Based off of my trial and error, the answer appears to be no, and that a simple template class Class<type>;

Explicit Instantiation of Template

て烟熏妆下的殇ゞ 提交于 2021-02-05 07:11:05
问题 This thread has been really helpful, but I still have questions about this process that don't appear to be answered. To what extent must I explicitly instantiate a template? For example, if in my definitions file I am using templates on every function, friend class, operator overload, etc, must I instantiate each in a template instantiation file (the current method that I am using)? Based off of my trial and error, the answer appears to be no, and that a simple template class Class<type>;

deduction guides for std::array

ぃ、小莉子 提交于 2021-02-05 06:16:45
问题 I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array works. Regarding the definition of the standard the following is the declaration template <class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>; For example if in main a array created as std::array a{42,45,77} How the deduction takes place? Thank you 回答1: How the deduction takes place? It's simple. Calling std::array a{42,45,77} match array(T, U...) with T = decltype(42

C++ Function Template Formatting

半世苍凉 提交于 2021-02-05 06:12:05
问题 Just had a question about class templates: For the following code, the function runs totally fine, but I am confused as to why/how you can run the fill function without giving a class/type for the iterators (why you don't need to supply the iterator type): #include <vector> #include <iostream> #include <typeinfo> template<typename Iter> void fill(Iter first, Iter limit, int value){ while (first != limit) { *first = value; ++first; } } int main() { std::vector<int> vec1 {2, 4, 6, 1, 9}; fill

Why C++ template type match doesn't retrieve reference qualifier '&'?

点点圈 提交于 2021-02-05 05:56:12
问题 I've got the following program: #include<stdio.h> template<class T> void f(T t) { t += 1; } template<class T> void g(T &t) { t += 10; } int main() { int n=0; int&i=n; f(i); g(i); printf("%d\n",n); return 0; } I expect that because i is a reference to n , so I expect that the template function f should get int& for template type T . But in fact it doesn't. The output of the program is 10 , not 11 as I expected. So my question is, for f , why T matches int but not int& of variable i ? What's

“Nested” class-template argument deduction with parentheses: GCC vs. clang

佐手、 提交于 2021-02-05 04:59:20
问题 Related, but (IMHO) different: Nested template argument deduction for class templates not working The following C++17 code is rejected from GCC 8, but clang compiles it without any issues. The GCC's error message is included as a comment just before the problematic line. Which compiler is correct here? https://godbolt.org/z/WG6f7G template<class T> struct Foo { Foo(T) {} }; template<class T> struct Bar { Bar(T) {}; }; void works() { Bar bar{1};// {} Foo foo(bar);// () } void works_too() { Foo

Any way to initialize this variable based on class template type?

微笑、不失礼 提交于 2021-02-04 21:08:16
问题 I have a class stats with a template so that it can be flexible. I'm new to templates though, and I thought the point of them was to make it flexible around the user. So I feel like I'm doing something wrong seeing as I've hit a small wall. #include <iostream> #include <cstdio> #include <iomanip> template <typename T> class stats { private: int n; T sum; public: stats() { this->n = 0; this->sum = T(); } void push(T a); void print(); }; int main() { std::string tmp; // change type based on

Any way to initialize this variable based on class template type?

核能气质少年 提交于 2021-02-04 21:05:28
问题 I have a class stats with a template so that it can be flexible. I'm new to templates though, and I thought the point of them was to make it flexible around the user. So I feel like I'm doing something wrong seeing as I've hit a small wall. #include <iostream> #include <cstdio> #include <iomanip> template <typename T> class stats { private: int n; T sum; public: stats() { this->n = 0; this->sum = T(); } void push(T a); void print(); }; int main() { std::string tmp; // change type based on

Call void function for each template type in a variadic templated function?

你离开我真会死。 提交于 2021-02-04 20:05:29
问题 My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name function for any types, including multiple types at once (variadic): template <typename T> void print_type_name(void) { std::cout << typeid(T).name() << std::endl; } This works fine for things like this: print_type_name<int>(); print_type_name<std::string>(); print_type_name<std::vector<std:

Call void function for each template type in a variadic templated function?

南楼画角 提交于 2021-02-04 20:05:12
问题 My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name function for any types, including multiple types at once (variadic): template <typename T> void print_type_name(void) { std::cout << typeid(T).name() << std::endl; } This works fine for things like this: print_type_name<int>(); print_type_name<std::string>(); print_type_name<std::vector<std: