templates

Compile-time template `std::integral_constant` counter - how to implement it?

守給你的承諾、 提交于 2021-02-18 23:00:47
问题 I have several types and I want to "bind" an std::integral_constant sequential ID value to every type at compile-time. Example: struct Type00 { }; struct Type01 { }; struct Type02 { }; struct Type03 { }; struct TypeXX { }; struct TypeYY { }; template<typename T> struct TypeInfo { using Id = std::integral_constant<int, ???>; }; int main() { cout << TypeInfo<Type00>::Id::value; // Should always print 0 cout << TypeInfo<Type01>::Id::value; // Should always print 1 cout << TypeInfo<Type02>::Id:

using typedef in template instantiation and extern template declaration

穿精又带淫゛_ 提交于 2021-02-18 22:55:19
问题 There are two cases where typedef confuses me when it comes to extern template declaration and explicit template instantiation . To illustrate the two see below 2 example code snippets. Consider following example (Case 1) : // suppose following code in some cpp file template <typename T> struct example { T value; }; // valid typedefs typedef example<int> int_example; typedef example<std::string> string_example; // explicit instantiation using above typedefs template class int_example; // ->

Random string in template django

為{幸葍}努か 提交于 2021-02-18 22:28:36
问题 Is there any way to have a random string in a django template ? I would like to have multiple strings displaying randomly like: {% here generate random number rnd ?%} {% if rnd == 1 %} {% trans "hello my name is john" %} {% endif %} {% if rnd == 2 %} {% trans "hello my name is bill" %} {% endif %} EDIT: Thanks for answer but my case needed something more specific as it was in the base template (wich I forgot to mention sorry ) . So after crawling google and some doc I fall on context

Forward declaration of template friend function

时间秒杀一切 提交于 2021-02-18 22:10:22
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

Forward declaration of template friend function

社会主义新天地 提交于 2021-02-18 22:10:18
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

Forward declaration of template friend function

Deadly 提交于 2021-02-18 22:09:44
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

Will a good C++ compiler optimize a reference away?

家住魔仙堡 提交于 2021-02-18 20:08:48
问题 I want to write a template function that does something with a std::stack<T> and an instance of T , e.g.: template<class StackType> inline bool some_func( StackType const &s, typename StackType::value_type const &v ) { // ... } The reason I pass v by reference is of course to optimize for the case where StackType::value_type is a struct or class and not copy an entire object by value. However, if StackType::value_type is a "simple" type like int , then it's of course better simply to pass it

Will a good C++ compiler optimize a reference away?

梦想的初衷 提交于 2021-02-18 20:06:36
问题 I want to write a template function that does something with a std::stack<T> and an instance of T , e.g.: template<class StackType> inline bool some_func( StackType const &s, typename StackType::value_type const &v ) { // ... } The reason I pass v by reference is of course to optimize for the case where StackType::value_type is a struct or class and not copy an entire object by value. However, if StackType::value_type is a "simple" type like int , then it's of course better simply to pass it

ES6 JavaScript template literals - What they can and can't do

ⅰ亾dé卋堺 提交于 2021-02-18 19:00:38
问题 Template literals make string manipulation much easier. However: What can and what can't they do in comparison to template libraries such as mustache and handlebars? I found it difficult to find an answer to these questions: Can they handle conditions? Can they handle loops? Can they handle functions? 回答1: The name is a bit ambiguous, but template literals do not replace template engines. They role is only to provide a more convenient syntax to work with strings. In fact, the objective was to

ES6 JavaScript template literals - What they can and can't do

老子叫甜甜 提交于 2021-02-18 18:59:38
问题 Template literals make string manipulation much easier. However: What can and what can't they do in comparison to template libraries such as mustache and handlebars? I found it difficult to find an answer to these questions: Can they handle conditions? Can they handle loops? Can they handle functions? 回答1: The name is a bit ambiguous, but template literals do not replace template engines. They role is only to provide a more convenient syntax to work with strings. In fact, the objective was to