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.
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.
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)?
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?
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.
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.
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.
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:
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;
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?
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<class T> class Singleton; class Base; class Sub : public Base, public Singleton<Sub>;
I' using underlying auto pointers, that's why Singleton is a template class and Sub passes itself as a template parameter. I'm developing Singleton and Base and a public API allows anyone to add their own sub classes. I actually want a real triple hierarchy like this:
template<class T> class Singleton; class Base : public Singleton<Base>; class Sub : public Base;
So that external developers don't have to worry about templates and complexity. The problem with this is that my implementation in Singleton will now call the constructor of Base whenever I create an instance of Sub (since the template parameter is Base).I was wondering if this could be done by pre-processor macros:
template<class T> class Singleton; class Base : public Singleton<__CLASS_NAME__>; class Sub : public Base;
Where __CLASS_NAME__ is the class name that will be replaced by the pre-processor. Theoretically this should be possible, since the __PRETTY_ FUNCTION__ macro actually returns the class name. The problem is that one cannot do string-manipulation to remove the function name from __PRETTY_FUNCTION__.
how I can accomplish this so that the Sub class is not aware of inheriting from a Singleton<template> class?
But when I try to build it, I get this error on line 24:could not convert template argument 'lambda' to 'void (*)(const string&) {aka void (*)(const std::basic_string<char>&)}'|
I thought the lambda expression I wrote would decay to a function pointer matching the template parameter. I can guess that the constexpr qualifier might have changed the type, but without it my compiler complains that lambda needs to be declared as constexpr...
So is there a way to pass lambda expressions as template parameters?
T1 will never be 'nothing' T2 will only be 'nothing' if T3 also is 'nothing' (if it works with T3 not being nothing, that's fine, but it won't get used that way).
Portability is a non-issue, this only needs to work in VS (2010 and higher). The 'real' solution will need up to T10, I have a solution working with SFINAE, but it takes very very long to compile and it's getting very unwieldy if you would need to add T11.
I know it's not an ideal type approach, but it is what it is, this is a necessity due to linking with a legacy API which we don't have control over.
Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream> #include <string> using namespace std; class Obj { public: string name;
[Code] ....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
I'm looking for a way to generate a program-wide unique value to use as a template parameter. Generating a unique value within a translation unit is pretty easy with __LINE__, but that doesn't ensure uniqueness across translation units. I thought maybe I could use __FILE__, but that can't be used as a template parameter.
I stumbled across this page: [uRL] ....
which is exactly what I want, except that the anonymous namespace trick doesn't work on all the compilers I've tried it on. (This may be due to C++11 changing anonymous namespaces to internal linkage rather than external as they did before....that page is five years old.)
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream> #include <string> using namespace std; class Obj { public: string name; Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
What I want to do is get all characters in a string up to a certain delimiter, for that I've searched I can do that with:
sscanf getline strok
But what I don't know and haven't found is how to use these functions but to start reading from a certain position in the string. So I need to get the characters up to a comma but after "THIS," so that would be starting in position 5...
How could I do that? I tried using [x] in brackets but then it would just read a character...
Calculated by the explicit scheme. Produces some very large numbers.
task:
[math] U_t = 3 (1,1-0,5 x) U_ {xx} + e ^ t-1 [/ math] [math] U (0, t) = 0 [/ math] [math] U (1, t) = 0 [/ math] [math] U (x, 0) = 0.01 (1-x) x [/ math]
Need to find a solution with accuracy [math] 0.0001 [/ math] on the interval [math] T = 1 / a ^ *, where a ^ * = max a (x, t) [/ math] Plot graphs of functions [math] u (x ^ *, t), u (x, jt ^ *) [/ math] where [math] x ^ * = 0.6, t ^ * = T/10, j = 1,2,4 [/ math]