templates

Accept templated parameter of stl_container_type<string>::iterator

I have a function where I have a container which holds strings (eg vector<string>, set<string>, list<string>) and, given a start iterator and an end iterator, go through the iterator range processing the strings. Currently the function is declared like this: template< typename ContainerIter> void ProcessStrings(ContainerIter begin, Co...

Is it possible to have a function pointer to a template function in c++?

I want to know if it's possible to create a template function and then create a function pointer that points to that template function. Is this possible? I'm using VS2008. The following code gets this compile time error: "cannot convert from 'overloaded-function' to 'int (__cdecl *)(int &,int &)' None of the functions with this name ...

Template function in define

Hello, I have some template function and I want to call it using define in c++: #define CONFIG(key, type, def) getValue<type>(key, def); Of course, it won't work. Could I make something like this? ...

How do i use 'auto' in C++ (C++0x) ?

What do i have to do to this code to make it compile, it's braking around this line: auto val = what.getObject(); #include<iostream> using namespace std; class CUP{ public: void whatsHappening(){} }; class MUG{ public: void whatsHappening(){} }; class CupThrower{ public: CUP cp; CUP getObj...

TinyXML and fetching values

Hello, I'm trying to load data from xml-file with TinyXML (c++). int height = rootElem->attrib<int>("height", 480); rootElem is a root element of loaded xml-file. I want to load height value from it (integer). But I have a wrapper function for this stuff: template<typename T> T getValue(const string &key, const string &defaultValue ...

Android Template Application?

I have built an application that I want to use as the foundation for a few other variants. The variants will come from assets / resource files and a unique AndroidManifest.xml. However, I want to be able to leave all the application code alone (modifying the package of all my classes, etc). I'm having a hard time figuring out how to do s...

C++: Templates for static functions?

I have a static Utils class. I want certain methods to be templated, but not the entire class. How do I do this? This fails: #pragma once #include <string> using std::string; class Utils { private: template<class InputIterator, class Predicate> static set<char> findAll_if_rec(InputIterator begin, InputIterator end, Predicate ...

When does a const return type interfere with template instantiation?

From Herb Sutter's GotW #6 Return-by-value should normally be const for non-builtin return types. ... Note: Lakos (pg. 618) argues against returning const value, and notes that it is redundant for builtins anyway (for example, returning "const int"), which he notes may interfere with template instantiation. While Sutte...

Strange overloading rules in C++

I'm trying to compile this code with GCC 4.5.0: #include <algorithm> #include <vector> template <typename T> void sort(T, T) {} int main() { std::vector<int> v; sort(v.begin(), v.end()); } But it doesn't seem to work: $ g++ -c nm.cpp nm.cpp: In function ‘int main()’: nm.cpp:9:28: error: call of overloaded ‘sort(std::vector<...

Virtual functions and templates in C++ - can they be replaced with other (existing in C++) operations?

For example concept of Templates in C++ are for comfort as compiler generates some additional code, for your class or for your function, isn't it? So we could live without template by doing some additional (manual work). What about virtual functions??? Are there any situations where both of them are irreplaceable? ...

C++ member template for boost ptr_vector

Hi there, I'm trying to write a container class using boost::ptr_vector. Inside the ptr_vector I would like to include different classes. I'm trying to achieve that using static templates, but so far I'm not able to do that. For example, the container class is class model { private: boost::ptr_vector<elem_type> elements; public: vo...

C++: Trouble with dependent types in templates

I'm having trouble with templates and dependent types: namespace Utils { void PrintLine(const string& line, int tabLevel = 0); string getTabs(int tabLevel); template<class result_t, class Predicate> set<result_t> findAll_if(typename set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred) // warning C...

C++ ambiguous template instantiation

the following gives me ambiguous template instantiation with nvcc (combination of EDG front-end and g++). Is it really ambiguous, or is compiler wrong? I also post workaround à la boost::enable_if template<typename T> struct disable_if_serial { typedef void type; }; template<> struct disable_if_serial<serial_tag> { }; template<int M, ...

Loading velocity template inside a jar file

I have a project where I want to load a velocity template to complete it with parameters. The whole application is packaged as a jar file. What I initially thought of doing was this: VelocityEngine ve = new VelocityEngine(); URL url = this.getClass().getResource("/templates/"); File file = new File(url.getFile()); ve = new V...

C++: Trouble with templates (C2064)

I'm having compiler errors, and I'm not sure why. What am I doing wrong here: Hangman.cpp: set<char> Hangman::incorrectGuesses() { // Hangman line 103 return Utils::findAll_if<char>(guesses.begin(), guesses.end(), &Hangman::isIncorrectGuess); } bool Hangman::isIncorrectGuess(char c) { return correctAnswer.find(c) == strin...

How to access hidden template in unnamed namespace?

Here is a tricky situation, and i wonder what ways there are to solve it namespace { template <class T> struct Template { /* ... */ }; } typedef Template<int> Template; Sadly, the Template typedef interferes with the Template template in the unnamed namespace. When you try to do Template<float> in the global scope, the compiler...

Dynamically render partial templates using mustache

Is there a way to dynamically inject partial templates (and have it work the same way in both Ruby & Javascript)? Basically, I'm trying to render different types of objects in a list. The best I can come up with is this: <div class="items"> {{#items}} <div class="item"> {{#is_message}} {{> message}} {{/is_message}} {{#is_pictu...

django url from another template than the one associated with the view-function

Heyy there, i have for example a view function like this: def profile_view(request, id): u = UserProfile.objects.get(pk=id) return render_to_response('profile/publicProfile.html', { 'object_list': u, }, context_instance=RequestContext(request)) and the url: url(r'^profile_view/(?P\d+)/$', ...

C++ meta-splat function

Is there an existing function (in boost mpl or fusion) to splat meta-vector to variadic template arguments? For example: splat<vector<T1, T2, ...>, function>::type // that would be the same as function<T1, T2, ...> My search have not found one, and I do not want to reinvent one if it already exists. Alternatively, is there a solutio...

Checking if a function has C-linkage at compile-time [unsolvable]

Is there any way to check if a given function is declared with C-linkage (that is, with extern "C") at compile-time? I am developing a plugin system. Each plugin can supply factory functions to the plugin-loading code. However, this has to be done via name (and subsequent use of GetProcAddress or dlsym). This requires that the functions...