C++ ::  Writing A Function Template With Template Arguments?

Mar 14, 2014

I have a function:

template<class Iterator, class T>
void a(Iterator, Iterator, const T&);

and I want to be able to simplify calls to 'a' with calls like

a(someIteratableContainer);

instead of having to call:

a(someIteratableContainer.begin(), someIteratableContainer.end(), valueOfTheContainersElementType);

I also want to be able to generalize the function to handle any of the standard iteratable contains: array, vector, deque, whatever.

I was under the impression I could write:

template<template<class T> class U> a(U<T>& container) {
a(container.begin(), container.end(), g(T()));
}

where 'g()' returns an object of the element type. However, the compiler is claiming, no matter how I write a call to the overload, the original template is selected and/or the overload is invalid, depending on the various ways I attempt to write said overload.

View 7 Replies


ADVERTISEMENT

C++ :: Function With Template Arguments

May 26, 2013

I'm trying to create a callback wrapper for pointers to member functions of any class by using templates, std::function and std::bind. This will be used to send incoming sf::Event's to classes who register callbacks with an event manager. I based my code off of the example on this page: URL.....Here's what I have:

class EventCallback
{
std::function<bool(const sf::Event&)> func;

public:
template<typename T>
EventCallback(T* object, bool(T::*func)(const sf::Event&)) { func = std::bind(func, object, std::placeholders::_1); }
bool run(const sf::Event& evt) { return func(evt); }
};

[code]....

View 4 Replies View Related

C++ ::  how To Declare Template Function Inside Template Class

Dec 5, 2013

I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :

my code is something like :

template<typename _Type, std::size_t _Size>
class array {
public :

[Code] ......

but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?

View 6 Replies View Related

C++ :: Circular Dependency In Template Arguments

Nov 7, 2013

Today I faced a problem where I had circular dependency in my template arguments. I was trying to make a class hierarchy similar to:

template<class BType>
class A_base {
public:
BType* getB();
};

[Code] .....

Basically I had objects that were of type A<B<A<B<...

Basically I have a tree like structure of heterogeneous types that must facilitate two-way interactions where A's can call B's and B's can call A's. This structure is useful in many contexts the difference is the methods A and B provide are different in each of these contexts. Instead of adding the getA and getB and all the other connectivity methods in every version of A and every version of B, I wanted to create a base class that managed this automatically.

Another piece of advice was break up your code so there is a forward-only and backwards-only dependent types. This is not a complete solution because the two cannot know about the other and this does not really facilitate arbitrary two-way communication (where A calls B then B calls A back). It also makes the code more complicated in that I have two sets of objects and interfaces.

So the solution was to make the template arguments specific to the things I wanted to be flexible. The connectivity interface of A_base and B_base should be constant. Hence that cannot be in the template parameter. It was merely the traits that I wanted to make flexible so... I came up with this solution:

#include <iostream>
template<class aTraitType,class bTraitType>
class A;
template<class aTraitType,class bTraitType>
class B;

[Code] ....

Now this compiles and works great. The problem is that aObj and bObj cannot call their opposite within a trait method because print() does not know anything about the connectivity. So the solution there was to make traits an abstract base class. Then magically everything works!

#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;

[Code] .....

So this outputs the following. Clearly there is two-way communication!

Class A is not connected to B
Class B is not connected to A
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty

View 6 Replies View Related

C++ :: Enable If With Variadic Template Arguments

Sep 17, 2014

template <typename FIRST, typename... REST, typename std::enable_if<std::is_convertible<FIRST, Base*>::value>::type* = nullptr>
void foo (FIRST first, REST... rest) {}

that successfully allows me to enable the function foo() only if FIRST is convertible to Base*, but I also only want foo() enabled if each type in REST... meets the same condition. What is the syntax for that? If no such syntax exists, how to achieve that effect?

View 7 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Getting Type Alias From Template Class Without Specifying Arguments And Instantiating It

Feb 4, 2015

For example I want to get the type of std::vector<>::size_type without specifying a template argument.

Here's what I want to do:

std::vector<>::size_type x{5};

Is there any way this can be done?

View 4 Replies View Related

C++ :: Use Of Class Template Requires Template Argument List

Nov 6, 2013

Error1error C2955: 'DoubleLinkedListInterface' : use of class template requires template argument listdoublelinkedlist.h10
Error2error C2244: 'DoubleLinkedList<T>::DoubleLinkedList' : unable to match function definition to an existing declaration doublelinkedlist.cpp7

Error3 .cpperror C2244: 'DoubleLinkedList<T>::~DoubleLinkedList' : unable to match function definition to an existing declaration 12

.h

#pragma once
#include "DoubleLinkedListInterface.h"
#include "Node.h"
#include <iostream>

[Code]....

View 4 Replies View Related

C++ :: Partial Template Specialization With Template Class

May 27, 2013

I have a generic template class with another template in one of its types. Now I want to specialize one of its methods for a particular (template) class, which leads to a compile error, however.

Here is the example:

#include <stdio.h>
template<typename Type>
class Obj1 {
public:
void ID() { printf("Object 1, size = %zu

[Code] .....

GCC ends with:
:35:27: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Type, template<class> class O> class Foo’
:35:27: error: expected a class template, got ‘Obj2<Type>’

What is wrong with the specialization? Can it even be achieved and how (if so)?

View 1 Replies View Related

C++ :: Template With A Specialized Template Class Parameter?

Nov 2, 2014

how I want the code to look. Only problem is it doesn't work (Line 11). I have some experience with templates but I'm not a pro.

Basically I want the "Channels<3>" to be a type that I can use to specify a Cable with similar to vector<float/int> it would be Cable<Channels<2 or 3>>.

What have I messed up with the syntax?

#include <iostream>
#include <vector>
using namespace std;

[Code].....

View 4 Replies View Related

C++ :: Use External Template With Template Specialization?

Nov 17, 2013

[URL]

#include <iostream>
struct Outer {
template<typename T>
void go() {
std::cout << Test<T>::f() << std::endl;

[Code] .....

I have tried several variants on this code to no avail. Outer is in a header, along with the extern template statements, and the specializations after main are in their own cpp file. Main is in a different cpp file.

What do have to do to make this work? I cannot bring the definitions of f() into the header, and they will be different for different template parameters. Ideally, I want Test to remain a private member of Outer, though this can change if it's the only option.

View 1 Replies View Related

C++ :: Passing Same Template Value To Two Different Template Functions

Dec 11, 2014

I have been trying to get a hang on templates. I have the two following functions that that could be consolidated in a single template function:

void Attractor::updateFamilies(FamiliesController *_tmp, int _counter){

center.x = ofGetWidth()/2;
center.y = ofGetHeight()/3;
attractorCounter = _counter;
if(attractorCounter == 1){

[Code] .....

NotesController and FamiliesController have the same parent. The thing that I'm trying to grasp with templates is that is could something like:

template<class TYPE>
void Attractor::updateData(TYPE* *_tmp, int _counter){
center.x = ofGetWidth()/2;
center.y = ofGetHeight()/3;
attractorCounter = _counter;

[Code] ....

And then have another template function declaration for all the attractor functions where I pass the same template value as in the first one.

As you can see, I'm calling another functions inside called attractors(_tmp). I know that one way around it could be to get rid of that function and just do all the logic inside of each if statement. Is there any way to pass the same template function parameter within a template function parameter?

View 2 Replies View Related

C++ :: Template Class With Template Members

Feb 9, 2015

I have a class like this

PHP Code:
template<class X>
class A {
  X m_x;
public:
    X* foo();
    X* bar();
    //others are not related to X
}; 

I would like to get rid of

PHP Code: template<class X> 

For class level but still use it for members. Like this

PHP Code:
class A {
  X m_x;
public:
    template<class X>
    X* foo();
    template<class X>
    X* bar();
    //others are not related to X
}; 

However, I am still stuck at

PHP Code: X m_x; 

View 6 Replies View Related

C++ :: Declare Template Type Object Inside Template Type Class Definition

Oct 12, 2013

Let me put it into the code snippet:

/**
This class build the singleton design pattern.
Here you have full control over construction and deconstruction of the object.
*/
template<class T>
class Singleton

[Code]....

I am getting error at the assertion points when i call to the class as follows:

osgOpenCL::Context *cxt = osgOpenCL::Singleton<osgOpenCL::Context>::getPtr();

I tried commenting assertion statements and then the debugger just exits at the point where getPtr() is called.

View 7 Replies View Related

C++ :: Vector Of Template In Template

Aug 29, 2014

Given:

#include <string>
template <class T>
class basic_container{

[Code] .....

is this possible:

#include "../basic_container/basic_container.hpp"
template <class T>
class container_vector{

[Code] .....

If not, what do I need to do to achieve my goal?

View 1 Replies View Related

C++ :: Function In A Class Template

Mar 3, 2013

I have this class templates And This UML.I have to write this function +operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE> but I do not know how to start the declaration / or start the function. I have to return a template but I do not know how to do it,

UML
Array<ElemType, SIZE>
-elements: ElemType[SIZE]
+Array()
+Array(source: Array<ElemType, SIZE>)
+operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE>
+operator==(other: Array<ElemType, SIZE>): Boolean
+operator!=(other: Array<ElemType, SIZE>): Boolean
<<L-value>>+operator[](index: Integer): ElemType
<<R-value>>+operator[](index: Integer): ElemType

[code]....

View 4 Replies View Related

C++ :: Template Function Of A Class

Feb 3, 2013

I want to use a template function of a class.

This is my code:

#include "Comparison.h"
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
Comparison c;

[Code] ....

But I get the error message:

main.cpp:10: undefined reference to `int Comparison::max<int>(int, int)'

View 2 Replies View Related

C++ :: Overload With Template Function

Nov 15, 2013

I am a beginner, got confused about:

template<typename T> int compare(T &a,T &b);
int compare(const char *a,const char *b);
char ch_arr1[6]="world",ch_arr2[6]="hello";
compare(ch_arr1,ch_arr2);

After running the code above,we got to know the non-template function is called. What I know is that the array arguments ch_arr1,ch_arr2 will not be converted to char * because the parameters are references in the template functions.

ch_arr1,ch_arr2 need to be converted to const char * if compare(const char *a,const char *b) were called.

I just wanna know what exactly happened behind that? and why?

View 15 Replies View Related

C++ :: Template Function In Struct

Nov 6, 2014

I understand template functions and how to call them explicitly when no arguments are passed to them but can't seem to instantiate them explicitly when it resides within a class or struct.

struct Creator {
template <class T>
T * New(T * p) {
return new T;
}
};
Creator cr;
MyClass * pMy = cr.New<MyClass>(); //gives compile error

The compiler complains about MyClass (or any other) being used illegally in expression ...

View 6 Replies View Related

C++ :: Invoke A Template Function?

Sep 24, 2014

I wrote out this coding to deal with template functions, but how would I invoke the function and to display the statements which im trying to invoke?

#include <cstdlib>
#include <iostream>
using namespace std;
#include <string>

[Code].....

View 2 Replies View Related

C++ :: Passing Map Templates To Function Template

Feb 21, 2014

I was just wondering how is this generally resolved. Let say you have this large function that runs in two modes. In the first mode it evaluates the data passed to a function as a map the the second mode it fills the map. example:

Code:
template <typename Map, typename Int>
void func(Map & map, Int i){
int z = 0;
string zz;

[Code] ....

The point is i do not want to write a large function just to include different modes so i decided to set "i" to be a mode identifier. However when i want to compile my function given two modes i get an error since the modes are not recognized (obviously). if i pass map as

Code: map<int,int>
and mode 1 i get an error here :
Code: map[z] = z; besause map
Code: map[z] expects z to be an int not string and the other way around (though in practice this cannot happen since i set the modes). So am i restricted to writing my function for both modes separately (polimorf.) or there is a way to make my example work.

View 1 Replies View Related

C++ :: Template Function As Member Of Class

Apr 15, 2014

I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?

Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )

[Code] .....

View 4 Replies View Related

C++ :: Passing Pointer Into Template Function

Oct 14, 2014

I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. how to make the function accept the pointer.

//main
int main()
{
srand(unsigned(time(NULL)));
int size;
int *list;
int *listCopy;

[code].....

View 4 Replies View Related

C++ :: Passing A Function As Template Parameter

Dec 26, 2013

Pseudocode:
template<typename T /*, some parameter for member_function */>
class Foo {
public:
void someFunction() {
T t;
t.member_fuction(...);
} }

I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;

I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.

The only thing I can think of is to make a proxy structure, something like this:

template<typename T, T> struct proxy;
template<typename T, typename R, typename... Args, R (T::*member_function)(Args...)>
struct proxy<R (T::*)(Args...), member_function> {
R operator()(T &obj, Args.. args) {
return (obj.*member_function)(std::forward<Args>(args)...);
} }

Which would then allow me to do (for example) this:

Foo<std::string, proxy<void(std::string::*)(), &std::string::clear> >

when Foo is implemented like this:

template<typename T, typename member_function>
class Foo {
public:
void someFunction() {
T t;
member_function()(t);
} };

That implementation works for me.

View 10 Replies View Related

C++ :: Undefined Reference To Template Function

Oct 21, 2013

I am coding a RTS game but I cant compile my dataLoader function. It gives that errors when want to call it:

//Window size
int width;
int height;
if( !dataLoader<int>( width, "settings/resolution.txt", "width" ) || !dataLoader<int>( height, "settings/resolution.txt", "height" ) )

[Code] ....

View 7 Replies View Related

C++ :: Overload Template Function In A Class?

Jun 21, 2013

Firstly, is it legal to overload a template function in a class? This is what I did

class FILE_txt
{
public:
FILE_txt(const char* );

[Code]....

View 19 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved