Problems with file inclusion for variadic class - c++

I have a variadic template class and I'm trying to include my namespace which is in another file so that I can use a few functions from it in the class.
However, the file including the namespace also includes a using statement related to the class as I need the variadic class for a few functions in that namespace as well.
I have many errors trying to do this and was wondering if there is a way to achieve my goal...
Here is what I mean:
#include "VariadicClass.hpp"
#include <stdint.h>
using VariadicClass3 = VariadicClass<3, int>
namespace mynamespace {
int function1(VariadicClass3 param){return 1;}
int function2(){return 0;}
}
////////////////////////////////////////////
#include "MyNamespace.hpp"
#include <stdint.h>
template<std::size_t T_size, typename T>
class VariadicClass3 {
public:
//...
void function1(){
auto some_var = mynamespace::function2();
}
};

You have couple of typos in your posted code. Ignoring that for the time being, you can use forward declaration as shown below to remove the cyclic dependency:
MyNamespace.hpp:
#pragma once
#include <cstdint>
// Forward declare the class.
template<std::size_t T_size, typename T> class VariadicClass;
using VariadicClass3 = VariadicClass<3, int>;
namespace mynamespace {
// Use the class only in reference in declaration.
// If the implementation of the function needs to
// access to members of VariadicClass3, it needs to be
// in a .cpp file where VariadicClass.hpp can be #include'd.
int function1(VariadicClass3 const& param){return 0;}
int function2(){return 1;}
}
VariadicClass.hpp:
#pragma once
#include "MyNamespace.hpp"
template<std::size_t T_size, typename T>
class VariadicClass {
public:
//...
void function1(){
auto some_var = mynamespace::function2();
}
};

Related

Dynamic eigen declaration types using templates

I am writing a simple program to define systems that has vectors representing the states. I would like to have a declaration type of the vector of Eigen depending on the number of states in the derived class.
I tried to achieve this using templates on aliases, something like the code shown below
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
class A
{
public:
template <int T>
using StateVector = typename Matrix<double, T, 1>;
};
class B : public A
{
public:
int NUM_STATES = 5;
B(){
StateVector<NUM_STATES> a;
a.setIdentity();
cout<<a<<endl;
}
};
int main(){
B b;
}
I ultimately want to have a type that can be used in the derived classes. Is this possible?
With two minor changes, your code works fine.
First, there must be no typename keyword here:
template <int T>
using StateVector = Matrix<double, T, 1>;
Second, NUM_STATES must be a compile-time constant, i.e., either declare it as element of an enum or as static const int (or static constexpr int, if you prefer):
static const int NUM_STATES = 5;
Full working example on godbolt: https://godbolt.org/z/_T0gix

Instantiating nested template function in template class [duplicate]

This question already has an answer here:
Member function template of class template can't find definition despite explicit instantiation present. Doesn't link
(1 answer)
Closed 3 years ago.
How do you instantiate a template member function of a template class without making a dummy call to it?
So imagine a header: class.h
#pragma once
template<class T>
class A {
public:
template<class R>
T b(R r);
};
class.cc
#include <limits>
#include "class.h"
template<class T> template<class R> T A<T>::b(R r)
{
/* Some code */
return std::numeric_limits<R>::max() - r;
}
void dummy()
{
A<int> a;
a.b<short>(2);
}
And some test file:
#include <iostream>
#include "class.h"
using namespace std;
int main(){
A<int> a{};
auto ans = a.b<short>(2);
cout << ans << " " << sizeof(ans) << endl;
}
How do I force the compiler to compile A<int>::b<short>(short) without calling it in class.cc (and thus having a dummy function laying around) or putting everything in the header (and having to recompile alot of code all the time).
I've tried different forms of template A<int>; and template <> template <> int A<int>::b(short) in the cc-file but I can't make up the right syntax.
You can do it with this:
template int A<int>::b<short>(short r);

How to use templates while separating header and implementation files?

I'm trying to implement a Template while separating the Header and Implementation file.
I'm getting this error when building the project:
error C2955: 'Series': use of class template requires template
argument list
Header.h
#ifndef SERIES_H
#define SERIES
template <class T>
class Series {
private:
T var;
public:
Series(T v);
};
#endif
Implementation.cpp
#include <iostream>
#include "Header.h"
template <class T>
Series::Series(T v) {
var = v;
std::cout << var;
}
Main.cpp
#include <iostream>
#include "Header.h"
int main() {
Series<int> w(10);
}
The project builds successfully when everything is in one file
What do I need to do to make this work?

How to separate definition and declaration of child template class

If I have a template class defined as:
#ifndef A_HPP
#define A_HPP
template<class T>
class A
{
public:
int doSomething(int in, bool useFirst);
private:
template<int CNT>
class B
{
public:
int doSomething(int in);
};
B<2> first;
B<3> second;
};
#include "a_imp.hpp"
#endif
Now I can go about having a declaration for A::doSomething in an implementation header file like this
#ifndef A_IMP_HPP
#define A_IMP_HPP
template<class T>
int A<T>::doSomething(int in, bool useFirst)
{
if (useFirst)
return first.doSomething(in);
return second.doSomething(in);
}
#endif
However I do not know how I go about making the declaration for the child class's method. Is it even possible or do I have to do one of the other two ways I can think of doing this, namely defining the methods in the main header or declare the class outside of A.
Please note that I am using C++11 so if this is only doable in that it will still work for me, though a C++98 solution would be good for other people.
You can do it in the following way:
template <class T>
template <int CNT>
int A<T>::B<CNT>::doSomething(int in)
{
// do something
}
Not sure if this is what you're asking, but I guess you need something like this:
template<class T>
template<int CNT>
int A<T>::B<CNT>::doSomething(int in)
{
return /*...*/;
}
Note that the template keyword appears twice, first for the template parameters of A, then for the parameters of nested class B.

"templating" a namespace

I'd like to build something like this:
File 1:
template<typename Vector>
namespace myNamespace {
class myClass1{ myClass1(Vector v) {...} }
}
File 2:
template<typename Vector>
namespace myNamespace {
class myClass2{ myClass2(Vector v) {...} }
}
Of course this is not possible because you cannot template namespaces.
Instead I could use a struct instead of a namespace, but then I cannot spread the namespace functions over several files.
Is there any solution for such a problem?
PS: I know I could template the classes, but then I'd have to specify which vector type I want to use anytime I create a new class.
Following up on your comment:
Instead of writing
using namespace myNamespace<int>;
Just use templated classes and write this instead (or whatever variation):
typedef myNamespace::myClass1<int> myClass1Int;
typedef myNamespace::myClass2<int> myClass2Int;
I tend to think it's better to be explicit about what types are being used rather than trying to do something like import a particular instantiation of a namespace.
Can you more fully describe the problem that makes you think templated namespaces would be useful?
And remember you can always write a make_myClass1 free function to deduce the template type for you.
You cannot do that, but you can provide different namespaces and typedefs (not that I endorse it).
namespace template_impl {
template <typename V>
class myClass1_tmpl {...};
template <typename V>
class myClass2_tmpl {...};
}
namespace myns_Vector1 {
typedef ::template_impl::myClass1_tmpl<Vector1> myClass1;
typedef ::template_impl::myClass2_tmpl<Vector1> myClass2;
}
void foo() {
using namespace myns_Vector1;
myClass1 mc1;
}
Anyway, my classes have several template parameters.
Now I created this approach:
#include <string>
#include <iostream>
namespace myNamespace {
template<typename _integer, typename _string>
struct s {
typedef _integer integer;
typedef _string string;
};
template<class T>
class classA {
public:
static typename T::integer intFunc() { return 1; }
static typename T::string stringFunc() { return "hallo"; }
};
}
int main() {
using namespace myNamespace;
typedef s<int, std::string> types1;
typedef s<unsigned int, char*> types2;
std::cout << classA<types1>::intFunc() << std::endl;
std::cout << classA<types1>::stringFunc() << std::endl;
std::cout << classA<types2>::intFunc() << std::endl;
std::cout << classA<types2>::stringFunc() << std::endl;
}
and I think I'll combine it with Mark B's approach!
Cheers, guys!