Программы на C++. Форум разработчиков
-
Anonymous
Как объявить шаблон класса другом другого класса шаблона
Сообщение
Anonymous »
Я пытаюсь реализовать класс, похожий на кортеж. Все работает хорошо. Это мой код:
Tuple.hpp
Код: Выделить всё
#pragma once
template
class Tuple : private Tuple
{
using Base = Tuple;
public:
Tuple(Head head, Tail... tail) : Base{ tail... }, m_head{ head }{};
Head& GetHead()
{
return m_head;
}
Base* base()
{
return static_cast(this);
}
const Base* base() const
{
return static_cast(this);
}
private:
Head m_head;
};
template
class Tuple
{
public:
Tuple(Head head) : m_head{ head } {};
Head& GetHead()
{
return m_head;
}
private:
Head m_head;
};
template
struct select;
template
struct select : select
{};
template
struct select
{
using type = Head;
};
template
using Select = typename select::type;
template
struct TgetNth
{
template
static R& get(T& t)
{
return TgetNth::get(*t.base());
}
};
template
struct TgetNth
{
template
static R& get(T& t)
{
return t.GetHead();
}
};
template
Select get(Tuple& t)
{
return TgetNth::get(t);
}
main.cpp
Код: Выделить всё
#include
#include "Tuple.hpp"
int main()
{
Tuple tup{ 1.1, 10, "Hello world", 2.2 };
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78456643/how-to-declare-a-class-template-as-friend-of-other-template-class[/url]
1715285197
Anonymous
Я пытаюсь реализовать класс, похожий на кортеж. Все работает хорошо. Это мой код:
Tuple.hpp
[code]#pragma once
template
class Tuple : private Tuple
{
using Base = Tuple;
public:
Tuple(Head head, Tail... tail) : Base{ tail... }, m_head{ head }{};
Head& GetHead()
{
return m_head;
}
Base* base()
{
return static_cast(this);
}
const Base* base() const
{
return static_cast(this);
}
private:
Head m_head;
};
template
class Tuple
{
public:
Tuple(Head head) : m_head{ head } {};
Head& GetHead()
{
return m_head;
}
private:
Head m_head;
};
template
struct select;
template
struct select : select
{};
template
struct select
{
using type = Head;
};
template
using Select = typename select::type;
template
struct TgetNth
{
template
static R& get(T& t)
{
return TgetNth::get(*t.base());
}
};
template
struct TgetNth
{
template
static R& get(T& t)
{
return t.GetHead();
}
};
template
Select get(Tuple& t)
{
return TgetNth::get(t);
}
[/code]
main.cpp
[code]#include
#include "Tuple.hpp"
int main()
{
Tuple tup{ 1.1, 10, "Hello world", 2.2 };
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78456643/how-to-declare-a-class-template-as-friend-of-other-template-class[/url]