Questions tagged [templates]

29695 questions
5

votes
0

answer
35

Views

Template metafunctions to generate pybind11 bindings

I am trying to create python bindings for some C++ functions using pybind11. The functions are templated but in the python bindings, I need one of the template parameter to be a function argument. Currently the way I have it is a lot of repetition for each template parameter. enum MyEnum {E1, E2, E3...
Bilentor
24

votes
3

answer
1.3k

Views

Why doesn't the standard consider a template constructor as a copy constructor?

Here's the definition of copy constructor, [class.copy.ctor/1]: A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments...
geza
1

votes
2

answer
40

Views

How do i specialize my template for std::string

I have the following templated function template ::type* = nullptr > As getStringAs(const std::string& arg_name) { std::istringstream istr(arg_name); As val; istr >> val; if (istr.fail()) throw std::invalid_argument(arg_name); return val; } And i would like to use it like this: getStringAs('2.f')...
eucristian
1

votes
1

answer
41

Views

Overriding a template in Django

I'm using this library to add a sessions tab on my django project. I'm calling the module's template from my project using this line: Sessions It works, but now I'd like to style the page, and to do that I need to override the module's template with my own template. So, as the module says here, I in...
Jack022
1

votes
1

answer
39

Views

Why my ajax function are not working on infinite scrolling?

I am building a webpage having infinite scroll using waypoints.js with backend as Django.The problem is, my jquery and ajax functions are not working on newly generated content while they are working with content on first page. **EDIT:-**I am updating my code so that it will be easier to undertsand...
Gagan
1

votes
1

answer
46

Views

Perfect forwarding an STL container to a template class

Trying to use perfect forwarding to pass a sequence container to a class that is templated. For instance: template class A { public: using ContType = TContainer; //This works for R-value references only explicit A(ContType&& container) : internalContainer(std::forward(container)) {}; //This does no...
Harvey Ellis
1

votes
3

answer
187

Views

VS2019 + Xamarin.Forms = UWP/EXE dropped?

How do I get the UWP back into Xamarin.Forms Cross-Platform creation to have the templates ready to roll? After picking through the release notes, playing with VS2019's UI and a few hours of web searching, I have only found others posting about the problem in the preview versions. Some say UWP is go...
Juggern Ott
1

votes
2

answer
90

Views

C++ expression templates ambiguous operator overloading

Im trying to implement vector and matrix expression templates.Both have the operator+ overload but i get ambiguous operator error. How can i overload operators for both matrix and vector while retaining the effects of expression templates? Here is my matrix sum template: template class MatSum: publi...
Marko Taht
1

votes
2

answer
81

Views

Angular's ngIf showing else template before evaluating condition

I'm checking if there are any elements in array before showing them. If there aren't, a 'not available' message should be displayed. Class: public lists = []; public isLoading: boolean = false; ngOnInit() { this.getLists() } getLists() { this.isLoading = true; this.list.getShoppingLists().subscrib...
shAkur
1

votes
2

answer
57

Views

Definition and Initialization of a tuple whose components are of the same templated class, but with different specialisations

I am new to c++ metaprogramming. I tried to look at other answers, but I was not able to find one that could suit my problem. Or simply I was not able to apply it to my case. Here I will post a simplified version of the code, to highlight the main features which I would like to obtain. What I would...
Garo
1

votes
1

answer
82

Views

How to traversal recursive template class

I want to traversal multiple depth template class. in C++98 (before c++11). pesudo code. template std::string find_type(T *ptr); template std::string find_type(int *ptr) { return 'string'; } template std::string find_type(std::list *ptr) { return 'list'; } template std::string find_type(std::vect...
user212942
1

votes
1

answer
59

Views

why can't the compiler bind a const int to an rvalue reference?

Here is a subset of my code. Why can't it bind a const int to an rvalue reference? What I expect is that T will be deduced as int const & and the type of e as int const & && will be folded to int const &. template List::List(initializer_list il) { for (T const & i : il) insertAsLast(i); } template...
BAKE ZQ
1

votes
2

answer
61

Views

Selecting Variadic Type with Runtime Index using Lookup Table

Consider a variadic pack of types. It's possible to select and use one of those types indexed by a runtime value using a recursive indexing function and a lambda with an auto parameter as follows: #include #include #include template struct identity { using type = T; }; template struct variadic {...
rtek
1

votes
1

answer
30

Views

Store unexpanded parameter pack

Basically I have a variadic template function like so: template void foo(std::string message, Args... args) { //Some nice code } I now wanted to have a struct, which stores the values, which I use later to call this function. I tried it like this: template struct Foo { std::string message; Args args...
Josef Zoller
1

votes
2

answer
46

Views

Performing initialization of templated class using other templated classes in variadic arguments of constructor

I wanted to create a simple HTML dom builder in C++ and decided I would use a templated tag class to describe the type of tag this was. I already used other methods to create the DOM in C++ with some success, but the design wouldn't handle raw strings, so the move to a templated class may assist me...
J. Willus
1

votes
1

answer
32

Views

C++ - Template static method with templated parameter inside template class [duplicate]

This question already has an answer here: What are some uses of template template parameters? 9 answers I have these two classes: template class InputCSV { public: InputCSV(DataSet * ds) : ds(ds) {} virtual ~InputLoaderCSV() = default; DataSet * ds; }; and template struct DataSet { template stat...
Martin Perry
1

votes
2

answer
73

Views

How to define template parameters for a generic lambda argument? [duplicate]

This question already has an answer here: Constructing std::function argument from lambda 1 answer Explanation: CLion and it's standard compiler give me an error that the 'candidate template [is] ignored', when I write a lambda as parameter for a generic function that takes a lambda as argument. Th...
Lyras
1

votes
1

answer
45

Views

How to overload an operator == inside an other class

I have a class A In a template class B template class B I want to overload the ==operator of A inside the class B, because I don't want it to be overloaded outside the class how can I do that? I tried: 1. bool operator==(const key &a, const key &b) compilation result: too many arguments 2. friend op...
Gabriel
1

votes
1

answer
76

Views

Check if template method exists without relying on automated template deduction

Is it possible to write a tester if a template-method with a given signature is defined? The method is of following signature: template ReturnType get(std::string) { return std::declval(); } I searched here but all I found were mechanism relying on automated template deduction (e.g. How to test if...
user3520616
2

votes
1

answer
22

Views

Instantiating nested template function in template class [duplicate]

This question already has an answer here: Member function template of class template can't find definition despite explicit instantiation present. Doesn't link 1 answer How do you instantiate a template member function of a template class without making a dummy call to it? So imagine a header: clas...
Vilhelm
2

votes
1

answer
23

Views

Why does my operator=(T&&) template only bind to a const& but not a &&?

When writing an overloaded function from an rvalue reference and a const reference, you might have code duplication, so I sometimes do both with the same code. As shown here: #include #include struct A { template A& operator=(T&&) { if constexpr (::std::is_rvalue_reference_v) { ::std::cerr
bitmask
0

votes
1

answer
19

Views

Inner class of a template class friend function

I have a templated class A with an inner class B. I want to have a friend == operator. However, the following code does not compile. It says, couldn't deduce template parameter ‘T’ #include template struct A { struct B { T b; template friend bool operator == (const typename A::B& b1, const type...
Mochan
0

votes
2

answer
91

Views

mixing templates with polymorphism

class A { friend void foo(); virtual void print_Var() const{}; };// does not contain variable Var; template class B : public A { T Var; public: B(T x):Var(x){} void print_Var() const override { std::coutVar=='Hello Stackoverflow') {/* do something*/} } } Explanation: I want to iterate over array of...
pooo mn
0

votes
0

answer
6

Views

Django: create an admin section gathering info from multiple models

I've been asked to add in the admin site of a Django project a new section that will gather information from several models (as it were a Database view) but I'm not allowed to change or add tables/views in the db. Checking similar questions custom page for Django admin in SO, I've ended up trying to...
Manu
0

votes
0

answer
3

Views

Django: database filter output displays <QuerySet [<model: model object (value)>]>

I am new to django, i am trying to import the data from a database by using multiple filters. views.py from django.shortcuts import render from django.http import HttpResponse from .models import NodesdataArchive # Create your views here. def home_view(request, *args, **kwargs): data = Nodesdata...
selvam samymuthu
1

votes
2

answer
812

Views

Go , there is no parameter $1

Im trying to get the values of the database by this way. But the compiler throw me an error when I go to /myapps direction. Structure: type App struct{ Title string Author string Description string } Function: func myappsHandler(w http.ResponseWriter, r *http.Request){ db, err := sql.Open('postgres'...
hdraven
0

votes
1

answer
11

Views

Silverstripe 3 list all templates (.ss - files) in a specific folder

Is there a ready made function in Silverstripe 3 to get all templates living in a specific folder to have them in an Arraylist? if not, anybody done that before? my goal is to choose the template from a dropdown or radio-button-set. or should i do it with something like... $files = glob('/path/to/di...
Sepp Hofer
0

votes
0

answer
21

Views

Reduce number of queries made to render list of items with same in-line editable field

While rendering a table of items, if the value for field X is not defined, it is rendered as a select element. Django makes a query for each select element and these can add up and cause delays in large tables. What is the best way to reduce the number of queries? views.py from rest_framework impor...
silverstripes
1

votes
1

answer
445

Views

'CsrfViewMiddleware' object is not iterable

I am new to Django, and I just took over from another developer on this project. All I have done so far is clone the code from git and install the dependencies. Immediately after setting up the project, and running python manager.py runserver and going to localhost:8000/admin I get an error stating...
0

votes
0

answer
3

Views

django-hitcount - display the date and ip of each recorded “hit”

This is a noob question, so excuse the lack of understanding. I'm using python 3.6 & django 1.10.5. I've been given a test page that now uses django-hitcount to track the number of times a document is 'hit' - viewed and downloaded. This seems to be working OK. I now want to extend the code so that I...
user3354539
6

votes
2

answer
209

Views

Why does the following program not select the argument of the same type as the first template parameter?

I am trying to write a function such that f(args..) returns the first parameter of type T. The following program seems to always select the first specialization thus printing 97 (ASCII code of 'a'). Though the second one wouldn't require converting char to int. Could someone please explain the beha...
Abhishek Kumar
1

votes
1

answer
288

Views

Equivalent of using if .. else as an expression in the Django Template Language

In Python, there are two ways to use if and else: either for Boolean flow control, in which case it is used with colons and indentation, or as an expression on a single line as described in https://www.pythoncentral.io/one-line-if-statement-in-python-ternary-conditional-operator/. As far as I can t...
Kurt Peek
1

votes
2

answer
681

Views

Django: List Products of each Categories in a page

I have a few categories and I would like to list the products per category in the format below (categories is an FK to products): Category 1 bunch of products .... Category N bunch of products I have tried many ways but so far I only get the categories but not the products to show in my HTML. model...
Inconnu
1

votes
3

answer
88

Views

How to Write a Lambda Wrapping a Function with Optional Return Value

I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore...
Benjamin Bihler
1

votes
2

answer
32

Views

Django Query: How to find all posts from people you follow

I'm currently building a website with the Django Framework. I want on the homepage of my website to display all posts made by people the user is following. Here are the classes for Profile, Story and Follow: class Profile(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True) fi...
Frederik Vanclooster
1

votes
2

answer
50

Views

Different behavior across compilers for std::enable_if (dependent on Outer class template parameters)

I have a nested (Inner) class, for which I want to enable_if a constructor, depending on how many template parameters (Args) the enclosing class (Outer) has. I came up with the code below, only to find out that it compiles fine on some compilers, and on some not. #include #include template struct...
sthlm58
1

votes
3

answer
113

Views

Using template type to generate unique member names for that template type

This use case comes from wanting to implement a compile-time event bus data structure that only listens/registers/unregisters specifically for the provided template arguments. Starting with a naive implemention lets say we have the following classes AListener, AEvent, BListener, BEvent. I want my Ev...
Water
1

votes
1

answer
76

Views

Can I have const parameter packs?

Alright so Working on a simple logging system for the fun of it, came across an interesting issue. I thought it would be nice to be able to write something along the lines of: Log(Info, 'Result: {}', value); To give me a result like Result: 45 (Using fmt's formatting style here.) So I set up the fun...
Nemo
1

votes
2

answer
44

Views

Does public static functions of base class become member functions of derived class if base class is inherited publicly?

I am trying to run the following code but getting error. #include template class Base { public : static T& Get() { static T t; return t; } }; class Derived : public Base { private : Derived() {} // friend Base; //un-commenting this will make code work. }; int main(...
kapil
1

votes
2

answer
78

Views

Implicit conversion operator T() with template overloads using std::enable_if fail to compile

I am writing a class in C++17 which I would like overload conversion operator for floating types as well as for some custom types. Here is the reproducible example. There are many more conversions that I need to add with templates, but if I can resolve these, the rest should be similar. class A { };...
Imran

View additional questions