I think the best way to explain what i want to accomplish will be through code:
template<typename T>
struct Foo {
static void func(Bar<T> obj);
}; // ^ syntax error: identifier 'Bar'
template<typename T>
struct Bar {
T data;
};
To resolve this, you just need what's called a "forward declaration":
template<typename T>
struct Bar;
template<typename T>
struct Foo {
static void func(Bar<T> obj); //syntax error no more
};
template<typename T>
struct Bar {
T data;
};
At the point where Bar<T> is referenced first the compiler does not have any idea what it is. You just need to add a declaration, beforehand, and the full definition of the template can still appear later.
Related
I try to create a templated class, which saves the template argument as a member variable (msg_).
Now the input of the class should allow normal types like int and shared_ptr<int>.
template <typename T>
class Foo {
public:
Foo<T>() {};
private:
T msg_;
};
My problem is if the class gets initialized with shared_ptr<int>, the shared_ptr has to get initialized:
this->msg_ = std::make_shared<T*>();
I tried to solve it like this:
Foo<T>() {
if (std::is_pointer<T>::value) {
this->msg_ = std::make_shared<T*>();
}
};
, but the problem is that is has to be solved on compile time. Otherwise I can not compile.
Do you know any patterns, which solve this issue?
Thank you! and
Best regards
Fabian
You might have specialization
template <typename T>
class Foo {
public:
Foo() {}
private:
T msg_;
};
template <typename T>
class Foo<std::shared_ptr<T>>
{
public:
Foo() : msg_(std::make_shared<T>()) {}
private:
std::shared_ptr<T> msg_;
};
or create function to initialize (with overload)
template <typename> struct tag{};
template <typename T>
T foo_default_init(tag<T>) { return {}; }
template <typename T>
std::shared_ptr<T> foo_default_init(tag<std::shared_ptr<T>>)
{
return std::make_shared<T>();
}
template <typename T>
class Foo {
public:
Foo() : msg_(foo_default_init(tag<T>{})){}
private:
T msg_;
};
Using GCC 4.8.2 on Linux, I want to grant the factory method Create() access to the private constructor of class C, but I get "error: 'Create' was not declared in this scope" when attempting to declare a specialized friend. How do I get this to work without opening up the declaration to all types for B::Create()?
template <typename T> class A {
public:
class B;
template <typename U> class C;
};
template <typename T>
class A<T>::B {
public:
template <typename U> static void Create();
};
template <typename T> template <typename U>
class A<T>::C {
C() = default;
friend void B::Create<U>();
};
template <typename T> template <typename U>
void A<T>::B::Create() {
C<U>{};
}
int main() {
A<int>::B::Create<char>();
}
I think you have run into a compiler defect. The following code, that uses only one layer of templates, works fine in g++ 4.8.2.
class Foo
{
public:
template <typename U> static Foo* Create();
};
template <typename U> class Bar : public Foo
{
private:
Bar() = default;
friend Foo* Foo::Create<U>();
};
template <typename U>
Foo* Foo::Create() {
return new Bar<U>();
}
However, the same compiler fails to compile your code. One work around, which I think you have already tried, is to replace
friend B* B::Create<U>();
with
template <typename V>
friend B* B::Create<V>();
I just tested your code in MSVC++ and it was failing there too. I was able to fix it by just giving the friend declaration a more explicit name. Remember, you can provide fully qualified names anywhere!
I just replaced
friend B* B::Create<U>();
with
friend B* A<T>::B::Create<U>();
I'll fire up MinGW and see if it actually does what you expect it to do.
Let's say I'm creating a class for a binary tree, BT, and I have a class which describes an element of the tree, BE, something like
template<class T> class BE {
T *data;
BE *l, *r;
public:
...
template<class U> friend class BT;
};
template<class T> class BT {
BE<T> *root;
public:
...
private:
...
};
This appears to work; however I have questions about what's going on underneath.
I originally tried to declare the friend as
template<class T> friend class BT;
however it appears necessary to use U (or something other than T) here, why is this? Does it imply that any particular BT is friend to any particular BE class?
The IBM page on templates and friends has examples of different type of friend relationships for functions but not classes (and guessing a syntax hasn't converged on the solution yet). I would prefer to understand how to get the specifications correct for the type of friend relationship I wish to define.
template<class T> class BE{
template<class T> friend class BT;
};
Is not allowed because template parameters cannot shadow each other. Nested templates must have different template parameter names.
template<typename T>
struct foo {
template<typename U>
friend class bar;
};
This means that bar is a friend of foo regardless of bar's template arguments. bar<char>, bar<int>, bar<float>, and any other bar would be friends of foo<char>.
template<typename T>
struct foo {
friend class bar<T>;
};
This means that bar is a friend of foo when bar's template argument matches foo's. Only bar<char> would be a friend of foo<char>.
In your case, friend class bar<T>; should be sufficient.
In order to befriend another same-type struct:
#include <iostream>
template<typename T_>
struct Foo
{
// Without this next line source.value_ later would be inaccessible.
template<typename> friend struct Foo;
Foo(T_ value) : value_(value) {}
template <typename AltT>
void display(AltT &&source) const
{
std::cout << "My value is " << value_ << " and my friend's value is " << source.value_ << ".\n";
}
protected:
T_ value_;
};
int main()
{
Foo<int> foo1(5);
Foo<std::string> foo2("banana");
foo1.display(foo2);
return 0;
}
With the output as follows:
My value is 5 and my friend's value is banana.
In template<typename> friend struct Foo; you shouldn't write T after typename/class otherwise it will cause a template param shadowing error.
It's not necessary to name the parameters so you get fewer points of failure if refactoring:
template <typename _KeyT, typename _ValueT> class hash_map_iterator{
template <typename, typename, int> friend class hash_map;
...
The best way to make a template class a friend of a template class is the following:
#include <iostream>
using namespace std;
template<typename T>
class B;
template<typename T>
class A
{
friend class B<T>;
private:
int height;
public:
A()//constructor
A(T val) //overloaded constructor
};
template<typename T>
class B
{
private:
...
public:
B()//constructor
B(T val) //overloaded constructor
};
In my case this solution works correctly:
template <typename T>
class DerivedClass1 : public BaseClass1 {
template<class T> friend class DerivedClass2;
private:
int a;
};
template <typename T>
class DerivedClass2 : public BaseClass1 {
void method() { this->i;}
};
I hope it will be helpful.
I have a base class that is a template that looks like this:
template <typename T>
class Foo
{
public:
T bar;
};
What I'd like to do is introduce a template argument that can be used to control the access mode of the member bar.
Something like this:
template <typename T,bool publicBar=true>
class Foo
{
public:
// If publicBar is false, insert protected: here
T bar;
};
Is this possible?
Thanks.
Edit:
Many asked so, for those interesting in why I'm doing this, here's my real code!
// Class used for automatic setter/getter generation.
template <typename T,publicSetter=true>
class Property
{
public:
Property(){}
Property(T value):mObject(object){}
T operator()()const
{
return mObject;
}
public: // This is where I want the protected:
virtual void operator()(T newObject)
{
this->mObject = newObject;
}
private:
T mObject;
};
This can be done using partial template specialization:
template <typename T,bool publicBar>
class Foo
{
};
template <typename T>
class Foo<T,true>
{
public:
T bar;
};
template <typename T>
class Foo<T,false>
{
protected:
T bar;
};
The only gotcha here is that you would need to replicate the entire class for each specialization... UNLESS you wanted to make it on top of a base class, e.g.:
template <typename T>
class FooBase
{
//the goods go here
};
template <typename T,bool publicBar>
class Foo : public FooBase<T>
{
};
template <typename T>
class Foo<T,true> : public FooBase<T>
{
public:
T bar;
};
template <typename T>
class Foo<T,false> : public FooBase<T>
{
protected:
T bar;
};
Yes, this is possible using partial specialization. Whether it's advisable is a another question - for a start, this solution doesn't scale as you need 2^n specializations where n is the number of variables you're controlling the access of. And do you really want the interface of your class to change based on the value of a template parameter?
It seems like you're creating something difficult to maintain, difficult to understand and overly clever.
Nevertheless, if you decide this is a good idea, here's how you would do it:
template <typename T, bool publicBar=true>
class Foo
{
public:
T bar;
};
template <typename T>
class Foo<T,false>
{
protected:
T bar;
};
Yes, using explicit class template specialization:
template<bool B> class Foo;
template<> class Foo<true>
{
public:
int n_;
};
template<> class Foo<false>
{
protected:
int n_;
};
int main()
{
Foo<true> fa;
fa.n_;
Foo<false> fb;
fb.n_; // ERROR: protected
}
Seems like a really bad idea, though. Why would you want to?
I think you could do this with a template specialization. Totally untested code.
template <typename T,bool publicBar=true>
class Foo
{
public:
// If publicBar is false, insert protected: here
T bar;
};
template <typename T, false>
class Foo
{
protected:
// If publicBar is false, insert protected: here
T bar;
};
But really consider why you'd want to do this. public data is really dangerous for encapsulation, and protected is nearly so. A solution that's able to utilize a client-API will probably be more maintainable in the long-term.
How about
template<typename T>
struct FooBase {
T bar;
};
template<typename T, bool publicBar>
class Foo : public FooBase<T> {};
template<typename T>
class Foo<T, false> : protected FooBase<T> {};
This way you don't have to define bar a number of times but only once.
My question is w.r.t the following thread : specialize a member template without specializing its parent
I'm absolutely fine with the standard saying that it is illegal to do so. But i want to understand why is it illegal to do so? What would be impact had it been allowed?
Maybe because of something like this:
template <typename T>
struct foo
{
template <typename U>
struct bar
{
typedef U type;
};
};
template <typename T>
struct foo<T>::bar<int> // imaginary
{
typedef void type;
};
template <>
struct foo<float>
{
template <typename U>
struct bar
{
typedef U* type;
};
};
// is it void [foo<T>::bar<int>] or
// int* [foo<float>::bar<U>]?
typedef foo<float>::bar<int>::type ambiguous;
A sensible solution is to say "we'll make the entire thing explicit".