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
ADVERTISEMENT
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
Dec 9, 2014
I'm making a minimal spanning tree class that inherits a graph class, both are templated. This is my mstree.h so far:
#ifndef _MSTREE_H_
#define _MSTREE_H_
#include "graph.h"
namespace MSTreeNameSpace
[Code]...
and I keep getting these errors:
mst.h:9:25: error: expected template-name before ‘<’ token
class MST : public Graph<T>
^
mst.h:9:25: error: expected ‘{’ before ‘<’ token
mst.h:9:25: error: expected unqualified-id before ‘<’ token
View 3 Replies
View Related
Oct 23, 2013
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?
View 6 Replies
View Related
Jul 8, 2014
Code:
class A {
public:
template<class T>
ObjectsBase* createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
};
template<class T>
ObjectBase* A::createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
My goal is to create any ObjectBase* derived object and return it to class A....
View 6 Replies
View Related
Aug 1, 2014
Code:
bool CObject<class T>::Create(LPDIRECT3DDEVICE9 pDevice, T *pMesh, float fAnimSpeed) {
if(pMesh == NULL)
return false;
m_pDevice = pDevice;
m_pMesh = pMesh;
if(m_pMesh->GetAnimController() != NULL) {
DWORD dwNumAnimSet = m_pMesh->GetAnimController()->GetMaxNumAnimationSets();
In here, T can be from the class CMesh * or CSkinnedMesh *, both have signature of GetAnimController
This maybe a flaw in my design... But I want Create to accept any kinds of meshes.
Should I create an interface or abstract class above all of those?
I have added an IMesh class that every derived classes has to have the needed interface.
But since then, I have another question, I feel that it is very cumbersome to include a template argument in every class/prototype that is associated with CObject
Say
bool CreatePlanesFromObject(CObject *pObject, D3DXPLANE *pPlanes);
Requires me to do this
template<class T> bool CreatePlanesFromObject(CObject<T>* pObject, D3DXPLANE* pPlanes);
Any ways I can omit the template argument, because it doesn't make sense to know the Mesh type in other classes...
View 1 Replies
View Related
Jan 22, 2015
PHP Code:
class B {
public:
template<class T>
T foo(){}
};
class D1:public B {
[Code] ....
I have that code piece and would want it to work but it doesn't.
Error "Error2error C2993: 'double' : illegal type for non-type template parameter '__formal' ....
I have no choice and have to use double and float for template typename when specializing. I tried to wrap it up like this
PHP Code:
typedef struct DOUBLE {
DOUBLE(double d){ val = d; }
double val;
};
but it's of no use.
View 6 Replies
View Related
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
Feb 10, 2013
class IFoo {
virtual void Bar() = 0;
};
class FooAbstract {
virtual void Bar() {}
[Code] .....
How to call the Bar() method from FooTemplate in FooDerived::Bar()?
View 5 Replies
View Related
Apr 16, 2012
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.
parser.h
Code:
#ifndef ENGINE_SCRIPT_PARSER_H
#define ENGINE_SCRIPT_PARSER_H
#include <string>
#include "variable.h"
namespace ninja_game_engine {
[Code] ....
The exact makefile looks like this:
Code:
test:
g++
Code/Engine/Modules/Timer/timer.cpp
Code/Engine/Modules/list.cpp
[Code] ....
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.
Rest of the code located here: [URL] ....
View 3 Replies
View Related
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
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
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
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
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
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
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
Feb 2, 2015
I have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
View 2 Replies
View Related
Jan 31, 2013
I am porting code from windows using visual studio to mac with xcode.
Quite a lot of issue have a appeared, which is no surprise, one warning that keeps on appearing is Explicit Specialiszation cannot have a storage class:
enum SortReturnCodeEnum {LOWEST=1, HIGHEST=2, HIGHLOW = 3, IDENTICAL=4};
template<typename tKey>
static SortReturnCodeEnum DefaultCompare(typename ArgType<tKey>::ARGT Value1, typename ArgType<tKey>::ARGT Value2)
{
[Code]....
I could do a #define __GNUC__ but i was checking
View 3 Replies
View Related
Feb 3, 2013
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
View 2 Replies
View Related
Oct 7, 2014
I've this variadic template class implementation:
// 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.
View 4 Replies
View Related
Apr 19, 2014
I'm trying to make an templated class of List and trying to make a list of Students(another class) which I made. Its not working.
Code seems to work fine for pre-defined data types but it crashes when it comes to students. I also want to run sort function on list of students.
View 1 Replies
View Related
Mar 12, 2012
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);
}
View 1 Replies
View Related
Mar 8, 2014
I have a class where a method based on the type passed I should return a value.
prototype declared in the header file:
template <typename T>int getNum() const;
Code of the cpp file:
template <typename T> int class::getNum() const{
int c = 0;
for(int i=0;i<v.size();i++)
if(typeid(*(Pro*)v.at(i)) == typeid(T)) c++;
return c;
}
To invoke the method as I do:
ostream & operator << (ostream & os, Pro & obj) {
return os << obj.getNum();
}
View 4 Replies
View Related
Oct 24, 2013
I have been experimenting with variadic templates with the aim of caching a call to a class method by storing away the object pointer, method pointer and parameters. I've actually had some reasonable success but have now hit a stumbling block. I now wish to wrap my parameters in a simple template class when I cache them. My success is as follows:
Using variadic template functions to store these pointers and paremeters;
I'm able to pass a method pointer and unwrapped parametersI'm able to pass wrapped parameters on their own.I'm NOT able to pass a method pointer and wrapped parameters I set up a little prototype project to demonstrate the issue and added comments above the function calls to indicate the compilation results. Here is the code:
Code:
#include "stdafx.h"
//////////////////////////////////////////////////
// Basic class with a simple method
//////////////////////////////////////////////////
class MyClass {
public:
char Method( int i, float f ) {
return 'A';
[code]....
But I'm convinced it should take three arguments, the method pointer and two wrapped parameters. Visual studio even suggested it should as shown below:
View 3 Replies
View Related
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