templates

Jinja2 check if value exists in list of dictionaries

孤人 提交于 2021-02-09 00:26:33
问题 I am trying to check if a value exists inside a list with dictionaries. I use flask 1.0.2. See example below: person_list_dict = [ { "name": "John Doe", "email": "johndoe@mydomain.com", "rol": "admin" }, { "name": "John Smith", "email": "johnsmith@mydomain.com", "rol": "user" } ] I found two ways to solve this problem, can you tell me which is better?: First option: jinja2 built-in template filter "map" <pre>{% if "admin" in person_list_dict|map(attribute="rol") %}YES{% else %}NOPE{% endif %}

std::is_same equivalent for unspecialised template types

你离开我真会死。 提交于 2021-02-09 00:25:17
问题 In one project I have found a possibility to stay DRY as a lot of code except for some small parts could stay the same for template specialisations of a template. Here is a small working example what I'm currently doing to check which templated class I'm using: template<typename T> class A{}; template<typename T> class B{}; template<template<class> class C> void do_stuff() { if(std::is_same<A<int>,C<int>>::value) { // Do Stuff for A } else if(std::is_same<B<int>,C<int>>::value) // Do Stuff

Jinja2 check if value exists in list of dictionaries

你离开我真会死。 提交于 2021-02-09 00:24:58
问题 I am trying to check if a value exists inside a list with dictionaries. I use flask 1.0.2. See example below: person_list_dict = [ { "name": "John Doe", "email": "johndoe@mydomain.com", "rol": "admin" }, { "name": "John Smith", "email": "johnsmith@mydomain.com", "rol": "user" } ] I found two ways to solve this problem, can you tell me which is better?: First option: jinja2 built-in template filter "map" <pre>{% if "admin" in person_list_dict|map(attribute="rol") %}YES{% else %}NOPE{% endif %}

std::is_same equivalent for unspecialised template types

醉酒当歌 提交于 2021-02-09 00:22:32
问题 In one project I have found a possibility to stay DRY as a lot of code except for some small parts could stay the same for template specialisations of a template. Here is a small working example what I'm currently doing to check which templated class I'm using: template<typename T> class A{}; template<typename T> class B{}; template<template<class> class C> void do_stuff() { if(std::is_same<A<int>,C<int>>::value) { // Do Stuff for A } else if(std::is_same<B<int>,C<int>>::value) // Do Stuff

Nested class of class template can be “incomplete”

删除回忆录丶 提交于 2021-02-08 21:49:17
问题 I'm at a loss as to how to explain why it is valid to create the member inner in the class template OuterTempl<T> whereas it is illegal to do so in the untemplated class Outer . // Non-template version struct Outer { struct Inner; Inner inner; // incomplete type (I get this) }; struct Outer::Inner { }; // Template version template<typename T> struct OuterTempl { struct InnerTempl; InnerTempl inner; // OK ... Huh!? }; template<typename T> struct OuterTempl<T>::InnerTempl { }; int main() { }

Deducing pointer-to-member template arguments

隐身守侯 提交于 2021-02-08 21:22:11
问题 Consider classes with member variables, like these: struct A { int a; char b; }; struct B { double c; bool d; }; Is it possible to declare a template class that accepts as its template argument a pointer-to-member-object to any one of the members declared in the above classes? A class that accepts a generic pointer-to-member-object could be declared and used as follows: template<typename Class, typename Type, Type (Class::*Member)> struct Magic { }; // Usage: typedef Magic<A, int, &A::a>

Can't pass std::min to function, copy of std::min works

情到浓时终转凉″ 提交于 2021-02-08 15:48:19
问题 Passing std::min to a function does not compile. I copied the libcpp declaration of std::min into my source file and it works. What's wrong with the std version? Same happens with clang and gcc. Tested on godbolt: https://godbolt.org/g/zwRqUA #include <thread> #include <algorithm> namespace mystd { // declaration copied verbatim from std::min (libcpp 4.0) template <class _Tp> inline constexpr const _Tp& mymin(const _Tp& __a, const _Tp& __b) { return std::min(__a, __b); } } int main() { std:

Compile-time counter in template class

◇◆丶佛笑我妖孽 提交于 2021-02-08 14:57:59
问题 I have a compile-time counter that I used for years, inspired by these answers. It works in C++03/11, and as far as I tested, relatively well on major compilers: namespace meta { template<unsigned int n> struct Count { char data[n]; }; template<int n> struct ICount : public ICount<n-1> {}; template<> struct ICount<0> {}; #define MAX_COUNT 64 #define MAKE_COUNTER( _tag_ ) \ static ::meta::Count<1> _counter ## _tag_ (::meta::ICount<1>) #define GET_COUNT( _tag_ ) \ (sizeof(_counter ## _tag_ (:

Compiler discrepencies in deduction of template parameter of nested alias template

寵の児 提交于 2021-02-08 14:47:29
问题 Consider the following code snippet where we we're trying to deduce the template parameter to the function foobar() : struct Bar { static constexpr auto size = 5; }; template <class T, class=std::make_index_sequence<T::size>> struct FooList; template <class T, std::size_t... Idxs> struct FooList<T, std::integer_sequence<std::size_t, Idxs...>> { }; struct Wrapper { template <class T> using list_type = FooList<T>; }; template <class T> void foobar(typename Wrapper::template list_type<T>) { }

extern template & incomplete types

a 夏天 提交于 2021-02-08 14:19:27
问题 Recently when I was trying to optimize my include hierarchy I stumbled upon the file a.hpp : template<class T> class A { using t = typename T::a_t; }; class B; extern template class A<B>; which seems to be ill-formed. In fact it seems as if the extern template statement at the end causes an instantiation of A<B> which causes the compiler to complain about an incomplete type. My goal would have been to define A<B> in a.cpp : #include <b.hpp> template class A<B>; This way I avoid having to