Программы на C++. Форум разработчиков
Anonymous
Как объявить шаблон класса другом другого класса шаблона
Сообщение
Anonymous » 09 май 2024, 23:06
Я пытаюсь реализовать класс, похожий на кортеж. Все работает хорошо. Это мой код:
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]
Шаблон функции как параметр шаблона другого шаблона
Anonymous »
19 июл 2025, 03:10 » в форуме
C++
У меня есть некоторые шаблоны функций с одинаковой подписью, и я пытаюсь создать функцию прокси, чтобы вызвать их с разными типами.
Как правильно передать имя функции в функцию прокси?template
T function()
{
return T();
}
template
void proxy1(int...
0 Ответы
9 Просмотры
Последнее сообщение Anonymous
19 июл 2025, 03:10
Функция шаблона как тип шаблона другого шаблона
Anonymous »
19 июл 2025, 01:15 » в форуме
C++
У меня есть некоторые функции шаблона с той же подписью, и я пытаюсь создать прокси -функцию, чтобы вызвать их с разными типами.
Как правильно передать имя функции в прокси -функцию?template
T function()
{
return T();
}
template
void proxy1(int...
0 Ответы
7 Просмотры
Последнее сообщение Anonymous
19 июл 2025, 01:15
0 Ответы
157 Просмотры
Последнее сообщение Гость
22 сен 2023, 16:36
0 Ответы
56 Просмотры
Последнее сообщение Anonymous
06 июл 2024, 13:08
Передача шаблона класса в качестве аргумента шаблона шаблона (C++)
Anonymous »
19 сен 2024, 04:04 » в форуме
C++
В следующем коде шаблон класса HANDLER должен принимать один параметр шаблона F и один параметр шаблона шаблона P. Базовый класс IMPLEMENTATION зависит от параметров R и S. При объявлении шаблона класса HANDLER я должен передать аргументы шаблона...
0 Ответы
39 Просмотры
Последнее сообщение Anonymous
19 сен 2024, 04:04