templates

Allow template parameter of function pointer type to accept functions of any return type

对着背影说爱祢 提交于 2021-02-10 11:45:09
问题 Is there a way to allow a template parameter of function pointer type to accept a function of any (rather than a specific) return type, when the function's return value is not actually used? Here's an MCVE to illustrate what I mean: int returnInt(int) { return 0; } void returnVoid(int) { } template <int (*Func)(int)> struct foo { void bar(int x) { Func(x); } }; int main(int, char *[]) { foo<returnInt> a; // ok foo<returnVoid> b; // argument of type "void (*)(int)" is incompatible // with

Basic template linked list

会有一股神秘感。 提交于 2021-02-10 07:40:09
问题 #include <iostream> #include <string> using namespace std; template <class T> class Node{ friend class LinkedList<T>; private: T data; Node <T> *next; public: Node(); Node(T d); ~Node(); }; template <class T> Node<T>::Node(){ T data = 0; next = 0; } template <class T> Node<T>::Node(T d){ data = d; next = 0; } template<class T> Node<T>::~Node(){ delete next; } template <class T> class LinkedList{ private: Node <T> *head; public: LinkedList(); ~LinkedList(); void Push_Front(const T& e); }

Thrust reduce with tuple accumulator

耗尽温柔 提交于 2021-02-10 06:18:27
问题 I want to use thrust::reduce on a thrust::host_vector of thrust::tuple<double,double> . Because there is no predefined thrust::plus<thrust::tuple<double,double>> I wrote my own and used the variant of thrust::reduce with four arguments. Since I'm a good citizen I put my custom version of plus in my own namespace where I left the primary template simply undefined and specialized it for thrust::tuple<T...> . #include <iostream> #include <tuple> #include <thrust/host_vector.h> #include <thrust

std::enable_if for two different methods implementation (4 different cases)

泪湿孤枕 提交于 2021-02-10 06:14:27
问题 I need to implement two different methods for const and non-const types. I have manage to write working code but I do not understand why some of its flavors are OK and some of them are not. Here is simplified example and I would like to know why #1 works but #2 is not, and same regarding #3 vs #4: #include <iostream> #include <vector> template <typename T> class X { public: // #1 - works template<typename B = T, typename std::enable_if<std::is_const<B>::value, int>::type = 0> void foo() {std:

Error while creating object from templated class

社会主义新天地 提交于 2021-02-10 06:12:04
问题 I've been trying to find a way to sample random vectors from a multivariate normal distribution in C++, having both the mean vector and the covariance matrix, much like Matlab's mvnrnd function works. I've found relevant code for a class that implements this on this page, but I've been having some problems compiling it. I've created a header file that is being included on my main.cpp, and I'm trying to create an object of the EigenMultivariateNormal class: MatrixXd MN(10,1); MatrixXd CVM(10

Templates inferring type T from return type

霸气de小男生 提交于 2021-02-10 05:44:07
问题 I have a template as follows: template <class T> vector<T> read_vector(int day) { vector<T> the_vector; {...} return the_vector; } I would like to be able to do something like vector<int> ints = read_vector(3); vector<double> doubles = read_vector(4); Is it possible for C++ templates to infer the return type from when they're called, or should I just pass a dummy argument to the template with the type I want to the vector to have? The latter works but is messier. 回答1: #include <vector> struct

How to detect a noexcept method using SFINAE

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 05:29:25
问题 I'm asking about a variation of a (popular) question - detecting the existence of a method of a class. I've read many answers here in SO, and most of the (post C++17) solutions look like this: #include <type_traits> template<class ...Ts> struct voider{ using type = void; }; template<class T, class = void> struct has_foo : std::false_type{}; template<class T> struct has_foo<T, typename voider<decltype(std::declval<T>().foo())>::type> : std::true_type{}; Basically, we're letting the compiler

How to pass props from template to react root node?

被刻印的时光 ゝ 提交于 2021-02-10 05:11:49
问题 I have managed to render my component on a div on my template like so: Index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Example</title> </head> <body> {% load render_bundle from webpack_loader %}<h1>Example</h1> <div id="react-app"></div> {% render_bundle 'main' %} </body> </html> My react app: Index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; const rootElement = document.getElementById('react-app'); ReactDOM.render(<App />,

how to print many variables with there name and their corresponding value in c++?

有些话、适合烂在心里 提交于 2021-02-09 09:55:32
问题 #include <bits/stdc++.h> using namespace std; #define __deb(X...) (cout << "[" << #X << "]:" << X) template <typename... type> void debug(type &&... args) { ((__deb(args)), ...); } int main() { int a = 1, b = 3; debug(a,b); return 0; } I got output like [args]:1[args]:3 but I wanted output like [a]:1[b]:3 回答1: One way could be to quote all the macro arguments using #__VA_ARGS__ and parse that string in the C++ function. Example: #include <iostream> #include <sstream> #include <string>

Show subtotal excl. tax, add subtotal tax as separate row on Woocommerce checkout

ε祈祈猫儿з 提交于 2021-02-09 06:47:46
问题 Currently I have all my products set to include tax. Because of rules in my country I would like to have my subtotal without tax, then a line with the amount of tax being paid and then the total with all the taxes (which is already default) In my review-order.php the line <td><?php wc_cart_totals_subtotal_html(); ?></td> gets called. I would like to subtract the tax percentage (21%) from that so ( / 121 * 100). And then a new line which shows the full tax amount so (total - subtotal). 回答1: