templates

Send type into function using templates

北战南征 提交于 2021-02-11 06:29:31
问题 I have this function for finding key in map and return the right value with type: template< typename T > T Foo::findInMap(std::string key) { std::map<std::string, std::any>::iterator it = options.find(key); if (it != options.end()) { return std::any_cast<T>(it->second); } return nullptr; } the map looks like this: std::map<std::string, std::any> options; How can I call and modify the function to get the best out of C++17? 回答1: Returning a nullptr is not always possible. Modern C++ way of

Converting any struct to json automatic in C++

喜夏-厌秋 提交于 2021-02-11 06:24:43
问题 I am willing to convert any struct to json automatic without repeating code or variables in C++. Desired result: typedef JsonStruct<string, name, int, age> Person; // not have to be template... Person person; person.name = "Jacob"; person.age = 16; json personAsJson = person.toJson(); Person newPerson = Person::fromJson(personAsJson); assert(newPerson == person); I found this answer: https://stackoverflow.com/a/34165367/13277578 Which is really cool! but you still have to repeat the variable

Skip earlier implicit template parameter, but not later, in a template parameter list

ぃ、小莉子 提交于 2021-02-10 20:14:01
问题 I am just curious: On the following sample I show what I mean in place of /*implicit*/ . Is there a workaround to leave it blank? typename T cannot be first as you can see. template<typename C1, typename C2, typename T = decltype(typename C1::value_type() * typename C2::value_type())> T dot(const C1 &v1, const C2 &v2); int main() { std::vector<float> vec1; std::vector<double> vec2; // typical: auto result1 = dot(vec1, vec2); // auto -> double // avoid numerical unstable situations: auto

Skip earlier implicit template parameter, but not later, in a template parameter list

寵の児 提交于 2021-02-10 20:11:03
问题 I am just curious: On the following sample I show what I mean in place of /*implicit*/ . Is there a workaround to leave it blank? typename T cannot be first as you can see. template<typename C1, typename C2, typename T = decltype(typename C1::value_type() * typename C2::value_type())> T dot(const C1 &v1, const C2 &v2); int main() { std::vector<float> vec1; std::vector<double> vec2; // typical: auto result1 = dot(vec1, vec2); // auto -> double // avoid numerical unstable situations: auto

Django Edit Profile - 'bool' object is not callable

非 Y 不嫁゛ 提交于 2021-02-10 18:31:44
问题 So I just followed this tutorial to create a profile for a user and allow them to edit it but I get this error when I go and visit the edit page 'bool' object is not callable File "C:\Users\...\account\userprofile\views.py", line 64, in profile_edit_view if request.user.is_authenticated() and request.user.id == user.id: TypeError: 'bool' object is not callable models.py: from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save

Django: The page shows “(<django.forms.fields.BooleanField object at …>)” at variable location in a template

女生的网名这么多〃 提交于 2021-02-10 16:52:18
问题 I was trying include a checkbox on my template, but it showed me some text on the browser instead. I have a form (forms.py) that contains boolean field, e.g: Class MyForm(forms.Form): my_checkbox = forms.BooleanField(required=False, label='my_checkbox'), and I have the following in my templates/my_app/my_form_template.py: <form id="form" method="post"> {{ form.my_checkbox }} <form> In the browser after running the server, I've got: (<django.forms.fields.BooleanField object at

Django: The page shows “(<django.forms.fields.BooleanField object at …>)” at variable location in a template

旧街凉风 提交于 2021-02-10 16:51:15
问题 I was trying include a checkbox on my template, but it showed me some text on the browser instead. I have a form (forms.py) that contains boolean field, e.g: Class MyForm(forms.Form): my_checkbox = forms.BooleanField(required=False, label='my_checkbox'), and I have the following in my templates/my_app/my_form_template.py: <form id="form" method="post"> {{ form.my_checkbox }} <form> In the browser after running the server, I've got: (<django.forms.fields.BooleanField object at

How do I make a functor out of an arbitrary function?

为君一笑 提交于 2021-02-10 16:21:16
问题 I have a bunch of functions which I want to use as functors (that is, use types instead of pass pointers to the functions, or any other kind of data). Is there an elegant/idiomatic/standard way of doing this with the standard library, or the standard library + Boost? Perhaps using bind() somehow? Or should I go with something simplistic (well, kind of simplistic) such as: template<typename Function, Function& F, typename... Parameters> struct functor { using function_type = Function; using

Using the upcoming C++ reflection facilities to print the full name of a type

ぐ巨炮叔叔 提交于 2021-02-10 14:46:43
问题 Currently, one can use __PRETTY_FUNCTION__ to display a template type under gcc , and clang : #include <iostream> template <class T> void print_type() { std::cout << __PRETTY_FUNCTION__ << std::endl; } int main(int argc, char* argv[]) { print_type<const volatile int&>(); return 0; } Will output: void print_type() [with T = const volatile int&] With the reflection TS coming and reflection facilities in C++, I was wondering what the syntax would look like to be able to do something similar.

How to automate Android project skeleton creation? (no Android Studio)

别说谁变了你拦得住时间么 提交于 2021-02-10 14:35:31
问题 I would like to create a regular Android project skeleton (e.g. create directories for all the sources, resources, flavors dimensions, etc; create manifest and xml stubs) I know you can do this in a shell script, but I want to use Gradle, if it is possible at all. Is that functionality built into Gradle, or do I have to write my own extension/plugin. How does Studio create the project skeleton in actuality? I don't want to re-invent the wheel, but I do not want the IDE. 回答1: Is that