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


ADVERTISEMENT

C++ :: Partial Specialization Of Template Parameter

Nov 20, 2013

I've been trying to create a templated class that takes a template as a parameter. I'd like to specialise this class for certain partial specializations of the template parameter but can't seem to figure out how to do it nor find anything online, (although I may be searching for the wrong thing).

As an example, say I have a class A that takes a template class with two parameters as its parameter:

template< template<class X, class Y> class Z > class A {};

I'd like to have a general version of A, for a general version of Z, but a specialisation of A for a specialisation of Z, e.g. where X is int but Y is still any type.

View 6 Replies View Related

C++ :: Not Picking Template Partial Specialization?

Nov 18, 2013

[URL]

#include <iostream>
#include <type_traits>
template<typename T, typename = void>
struct Test {
static int constexpr value = 1;

[Code] .....

Why does it output 1 instead of 2? How can I make it output 2 and still output 1 for e.g. Test<double>::value?

View 3 Replies View Related

C++ :: Template Partial Specialization - Combining Two Types Of Resources

May 15, 2013

I want to write a template that combines two type of resources:

class someClasses {
typedef someType ElementType;
} template<class T1,class T2>
class combo {
...
}

And I want to specify my T1 and T2 has the same ElementType, how can I write my combo class to partial specialize the general case so the ElementType check is at compile time?

View 9 Replies View Related

C/C++ :: Template Specialization Of Single Method From Templated Class?

Jan 21, 2013

I want to specialize a particular function for Integer datatype inside a class template. My XX.h will be

namespace ZZ {
       template <class T>
       class XX {
           void doSomething(T x);
       };
}  

provide me XX.cpp which has template specialization for the function doSomething on Integer datatype.

I tried the following in XX.cpp

#include "XX.h"
using namespace ZZ;  
template <class T>
void XX<T>::doSomething(T x) {

[Code] ...

But this code is not working.

View 2 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++ :: Nested / Recursive Template Specialization

Feb 11, 2014

I'm trying to write some naive binary serialization code and wanted to cut down on repetition of logic for serializing/deserializing nested vectors or other STL containers to reduce the chance of typos etc, and thought templates might hold a solution for me.

Code:
template <typename T> void serializeField(IWriter& writer, const T& val) {
writer.write((char*)&val, sizeof(T));
}

template<typename U, typename V>
template <> void serializeField(IWriter& writer, const U<V>& collection)

[Code] ....

Is there a way to do something like this? It isn't a big deal for me to just manually write code to serialize my vectors to the needed depth, but it sure would be nice to get this working.

View 10 Replies View Related

C++ :: Template Specialization For Char Arrays

Jun 8, 2012

I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:

Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {

[Code] ....

View 4 Replies View Related

C++ :: Template Specialization Does Not Work For User-defined Object

May 23, 2014

Can you take a look why I am getting compile error.

Code:
// clang++ main.cpp -g -std=c++11 -o main
#include <iostream>
class QuoteClass {
public:
QuoteClass() = default;
QuoteClass(std::string param) {symbol = param;}
std::string get_symbol() {return symbol;}

[Code] ....

View 5 Replies View Related

C++ :: Floating Point Template Parameters (called Specialization)

Sep 12, 2012

This code snippet just won't compile in a conforming compiler:

Code:
template<> struct series<0.0, 0, 0>

Because of the floating point template parameter.... If this "specialization" is useful, how come MS would have discarded it?

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++ :: 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++ :: 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++ :: 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++ ::  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 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++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C/C++ :: Template Class Is Not A Class Template

Feb 4, 2014

std::cout << "Hello C++ programmers!" << std::endl;

I am trying to create a LinkedList (and then, an ADT stack; // yes, I cannot use the STL stack because the teacher won't let me), and I am getting some weird error when I create a ListNode and declare LinkedList (which has the ListNodes!) a friend of it.

Here is my header-file code for both classes:

ListNode.h

#ifndef LISTNODE_H
#define LISTNODE_H
#include "LinkedList.h"

[Code]...

The errors I am getting are:

error: 'LinkedList' is not a class template

I have tried forward-declaring LinkedList in the ListNode.h file, but I get this error:

error: 'ListNode' does not name a type

Are there any other possible solutions to this problem; // without having to resort to crazy stuff like having a .h file #include a .cpp file, or even declaring and defining ALL OF MY CODE in the .h files???

View 6 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++ :: Template Class With Default Value?

Mar 22, 2013

refer to the code below, the attribute class is created with the value type and default value, but why it doesn't work for std::string?

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

[Code].....

View 9 Replies View Related

C++ :: Using Constructors In Template Class?

Sep 13, 2014

The code below references to a header file and implementation .cpp file, which are not important. My question is what is the proper way to use a constructor in a main file. I have been getting "invalid use of" errors when using letters.Pair(a,b), where Pair(T a, T b) is a constructor that accepts arbitrary type T of variables 'a' and 'b'. So I played around a bit and suddenly found a syntax that works. I need verification for the syntax below:

#include <iostream>
#include "pair.h"
#include "pair.cpp"

[Code].....

Are the comments with the asterisks correct? As in this is always the way you initialize and assign? So letters.Pair(a, b) is not the right way to use constructors?

View 2 Replies View Related

C++ :: How To Export Class With Template

Jun 3, 2014

I have the following class header in library, but when i initialize in main the class, i have an error unresolved external symbols. So the class is not exported as it should.

Code:

template <typename Key, typename Value>
class UTILITIES_EXPORT MyMap : public QMap<Key, Value>
{
public:
MyMap() : QMap<Key, Value>() { }

[Code]....

View 4 Replies View Related

C++ :: Implementing Stack Using Class Template?

Dec 5, 2013

The following program is designed to demonstrate class templates. The program will evaluate postfix expressions input to the console by pushing them in a stack, and then popping them and evaluating them as such, (ex. entering 3 4 + would equal 3+4 = 7).

The code is below. We are not to modify it, but to fill in the blanks, the places filled in indicated with two asterisks for a line, and one on each side for a part of a line. If I didn't know what to enter (if anything), I put three ?s. If you want to copy and compile for yourself, look for all the *s and ?s.

1) I'm turning up all sorts of errors in the main program file (prog5.cpp) having to do with stacktype.cpp. It has been removed from the program, as it is included at the end of stackType.h. Most of them are "cannot convert 'this' pointer from StackType to StackType<stack> &'. How do I fix that?

2) The program supposedly lacks a default constructor, and it keeps turning up that 's' is an array of unknown size (do I call StackType or stack or what?).

stackType.h Code: #pragma once// Catherine Stringfellow and Trey Brumley
// A Stack is a data type, which stores values in an order where values are added to the top of the stack when pushed,
// and when popped, remove and return the value from the top of the stack.
// Class specification for Stack ADT in file StackType.h
using namespace std;
static const int MAXITEMS = 50;

[code].....

View 11 Replies View Related

C++ :: How To Use The Operator In Class Template In Program

Apr 25, 2013

I have a list of numbers in an array created by a class template

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int CAPACITY = 20;
template <class T>
class A

[code].....

View 2 Replies View Related







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