C++ :: Unpacking Parameter Pack With Templated Predicate
Apr 20, 2014
I'm trying to generalize the following (working) code:
#include <iostream>
#include <string>
#include <cmath>
int foo (int num) {return num;}
int foo (double num) {return std::round (num);}
int foo (std::string str) {return str.length();}
[code]....
Template argument deduction failed, it says. And using pred_action<T> and pred_action<FIRST> only makes it worse.Do we need to use a visitor pattern or something like that?
let me jump straight to main() and my current output:
struct Base {}; struct A : Base { A() {std::cout << "A";} }; struct B : Base { B() {std::cout << "B";} }; struct C : Base { C() {std::cout << "C";} }; struct D : Base { D() {std::cout << "D";} };
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
For exemple:
Code: template <class T> class A { public: A() { version = ???? } std::string_base<T> version; };
I don't want to pass the default value as parameter of the constructor. how I can do this?
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream> #include <assert.h> using namespace std; const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function @param array: gives the array to be displayed @param array_size: gives the number of elements in the array */ void DisplayArray (int array[], int array_size);
bool validateNumber(string& text, int min = 0, int max = -1, bool useMin = true, bool getValid = true)
The code takes the string text, and checks the make sure that the input is valid and safe to convert and use as a number. However, sometimes there is not min, and sometimes there is no max. The lack of min is done by using the parameter useMin, while the lack of max is done by max < min.
My predicament is the following call: validateNumber(text, -2);
Now, max will be used, even though I don't want it. Ideally, I would want to do something like... int max = (min - 1), ... but that doesn't work. I also can't check to see if the parameter hasn't been changed (that I know of), because the following call would make it look like it hasn't validateNumber(text, -2, -1);
So the question is, is there a way to do what I want, without having to add in a bool useMax parameter? Or is this my only option? I don't want to do that for simplicity, but if I have to, I have to.
template <typename T> struct avl_tree { T data; int balance; struct avl_tree <T> *Link[2]; static int (*comp)(T, T); };
In main, I have a function like so: int compare(int a, int b) { return ( a - b ); }
Now how do I assign the function pointer in my avl_tree class to the compare function?
I did this: int (avl_tree<int>::*comp)(int, int) = compare;
But I got the compiler error: Tree_Test.cc: In function ‘int main()’: Tree_Test.cc:27:42: error: cannot convert ‘int (*)(int, int)’ to ‘int (avl_tree<int>::*)(int, int)’ in initialization
// Predefine template delegate factory template < typename R, typename... Args > class brGenericDelegate ; // C++11 template alias to announce functor definition template < typename R, typename... Args > using brGenericDelegateType = std::function< std::shared_ptr<R>(Args...) > ;
[code]....
This template works fine for me. Now I have to compare, if the functors of the generic templates are equals ore not. Actually I see no way to reach this aim, because I've to cast the brDelegate on each request.
Can I determine if a templated class has a particular constructor, in my case using a string within function to which T is used?
Code:
template<class T> void MakeObject(std::vector<T>& dataVector) { std::string str "con string,Joe,24"; // catch if T has string constructor T someObject(str); // T someObject should have constructor from string dataVector.push_back(someObject); }
This problem just seems really strange to me because it is simple yet for some reason my class cannot pass into another class. The class PASS_OBJECT has a static array (even with 1 element this doesn't work) and when I try to pass this class (after it is initialized) I seem to lose the data inside the PASS_OBJECT. Not only that but even when I declared the class OBJECT with the type of PASS_OBJECT<int> I seem to lose the integer 99. Here's the code, note that if you comment out line 89, 92 and 93 you will notice that line 90 outputs In main 2: 99 just fine but it doesn't otherwise???
#include <iostream> const int size = 1; template <class T> class PASS_OBJECT; template <class S> class OBJECT {
To generate output data, I'm printing a bunch of vector contents to files. Because the type of variable can differ between vectors, I wrote a templated printing function to print out whatever the content of the vector is. It looks like this:
I added the fixed because some larger values were being printed in scientific notation. Everything works well. My test code includes 3 vectors of doubles and 3 vectors of unsigneds. All the unsigneds work well and two of the doubles work well, but the third doubles vector prints nonsense unless I disable the fixed.
The calling code is the exact same. I know the values in the vector are correct, because a) if I comment out the "fixed" flag it works, and b) one of the unsigned vectors is sorted based on the values in that double vector (after it is printed, so the sort cannot corrupt the vector print) and works perfectly.
The "nonsense" looks like chinese/weird characters, if that matters.
I declared a member method to a class in its header file and implemented it in the cpp file. When I build and run the project in XCode, everything works fine. When I try to do it with a makefile, I get undefined symbols linker errors.
I'm pretty sure that there is a weird namespace gotcha that I'm unaware of that LLVM (default OSX compiler) is compensating for that g++ isn't. Or maybe something weird with the optimization? I want the tests running at that level to make sure everything that is volatile is declared as such.
On several occasions in my project, I need to sort elements (indeces) based on their linked values. Those values are stored in an array. This means the comparison looks like this:
bool operator()(int i, int j) { return someArray[i] > someArray[j]; }
Because this form returns often but someArray may differ, I wrapped this in a template class:
Now, I want to use this in a different way: I want to select the K indeces with the lowest value from someArray attached to it. My idea was to build a priority_queue, push the first K elements and then compare all the next elements to PQ.top(), as such:
INDEX t(0); for (; t < someLimit; ++t) { pq.push(t); if (pq.size() == K) break; } for (; t < someLimit; ++t) { if (someArray[t] < someArray[pq.top()]) { pq.pop(); pq.push(t); } }
My problem, however, is the definition / initialization of the priority_queue object. My first idea was simply std::priority_queue<INDEX> pq(sorter<VALUE>(someArray));, but calling any function on pq provides the error "expression must have class type" (?) when hovering over pq.
My second guess, std::priority_queue<INDEX, std::vector<INDEX>, sorter<VALUE>(someArray)> pq;, provides the error 'expected a ')'' when hovering over someArray.
What is the correct way to initialize this data structure?
I've been studying the heck out of the boost metafunction libraries. I understand a good deal of what things like varadic functions and integral sequence wrappers are, but I am having a hard time putting everything together to get working functions, such as performing arithmetic operations or functions like that of std::vector.
// Integral constant wrapper template<int T> struct int_
[Code] .....
My knowledge of all of this is pretty scattered and I've really been trying hard to put it all together. Is this correct? How can I apply this and use it to do more?
I need to create a templated doubly linked list, with an iterator class within the list class. This program is to function just like the STL list class but I only need to implement functions that I am using, My trouble is I am kind of clueless on the iterator part and the fact that the list is templated is giving me syntax grief. I have pasted the code I have done so far.
1. On the syntax implementing the list and iterator functions outside of the class 2. I am not sure when to deference the iterator in the functions, but think I have it right so far 3. For the reverse function can I copy the list into a new list in reverse then re add them to the original list overwriting the same values? I have the code I have so far there 4. For the iterator erase function, I am not sure if I am deleting the node correctly. 5. I am not sure if I need template <typename T> above the iterator functions. Does the iterator class need to be a template? Right now it is not.
// Templated doubly linked list class
#include <iostream> using namespace std; template <typename T> class list { private: Node *head; Node *tail;
I am working on building a set of templated data structures for my own learning and have run in to an error when instantiating my templated linked list. I receive the following error:
error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::~LinkedList<int>(void)" (??1?$LinkedList@H@@QAE@XZ) referenced in function _main
--LinkedList.h-- #ifndef LINKEDLIST_H #define LINKEDLIST_H template<class T> class LinkedList {
I'm trying to template the return type for this function (component), I've looked around for example code but there doesn't seem to be any exactly like what I want.
Entity.hpp class Entity { public: Entity(); unsigned int id = 0; Component& addComponent(std::string);
[Code] ....
Error : 'ent1.component<HealthComponent>' does not have class type
// RemoteControlMonitor.H typedef void (*keyaction)(unsigned int key);
class RemoteControlMonitor { private: keyaction rph; keyaction rrh;
[Code] .....
But I got compile error as below:
RemoteControlMonitor.H:58: invalid type `void *' for default argument to `void (*)(unsigned int)' rcx1.C: In function `void __static_initialization_and_destruction_0(int, int)': rcx1.C:54: ANSI C++ forbids implicit conversion from `void *' in default argument
int *buildTrail(int antIndex, int start, double *pheromones) { int *trail = new int[tabu]; bool *visited = new bool[tabu]; trail[0] = start; visited[start] = true;
[Code] ....
If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished? . . . delete visited ; return trail;
Unless I'm missing something, it's now possible(ish)? A little concept is below, very rough around the edges. Still though, if this is valid by standard C++, why can't we have built-in support for float / double template parameters?
I have run into a problem which is mostly just an annoyance. I need to know if i can have pass a derived class to a function which has the base class as its parameter. For example i have been creating a program but i have a function which needs to work for multiple classes all derived from the BaseObject class
Code :
class folder : public BaseObject {} class BaseObject {void function(BaseObject B)}