086 - Check it out! How Powerful Modern C++ Can Be, Matrix of Functions = std::any + std::tuple

preview_player
Показать описание
Thomas Kim's YouTube Channel

070 - Function of Three Variables, Normal Vector to 3D Surface and Gradient Vectors

079 - Newton Method, Five-Point Stencil, Gaussian Quadrature

084 - Rotation Matrix, Implementing Generalized Rotation Matrix and Vector 2

085 - Unprecedented Power of Modern C++, std::any + std::tuple = Vectors of Functions

Download Source Code:

Episode 086 - How Powerful Modern C++ Can Be, Matrix of Functions = std::any + std::tuple

Episode 085 - Unprecedented Power of Modern C++, std::any + std::tuple = Vectors of Functions

Episode 084 - Rotation Matrix, Implementing Generalized Rotation Matrix and Vector 2
Рекомендации по теме
Комментарии
Автор

영상 감사합니다
template<typename Type> class matrix { }; //primary class template
template<> class matrix<std::any> //specialization( 아무타입이나 다 받을 수 있음 (표준 any))
클래스의 멤버변수 (m_data) 는 std::vector<std::any> (아무 타입이나 다 저장할수 있는 vector) 임

template<int INDEX, typename FuncTypes, typename ReturnType,
typename ArgType, typename... ArgTypes>
static void extract(ReturnType& result, matrix& m, ArgType s, ArgTypes... args) //템플릿 파라미터 함수
반환 타입이 없고4개의 매개변수( result, m, s, args), 템플릿 타입 파라미터 는 ( 정수형, FuncTypes (람다), ReturnType(반환타입) , ArgType(람다함수의 첫번째 인자), ArgTypes (람다 함수의 첫번째를 제외한 variadic( 가변 인자, parameter pack) )

index를 하나씩 차감( INDEX-1), FuncTypes 하면서 재귀 호출하고 INDEX가 0이 되면 tfp_vector.hpp에 선언되어 있는 아래 함수(eval_function) 호출
template<typename ReturnType, typename ArgType, typename... ArgTypes, function_c<ReturnType, ArgType, ArgTypes...> FuncType>
ReturnType eval_function(FuncType&& f, ArgType&& a, ArgTypes&&... args)

{
return f(std::forward<ArgType>(a), //첫번째는 람다 함수이고 호출시 전달되는 람다 함수에서 첫번째 인자와 첫번쨰를 제외한 나머지 인자를 호출하고
그 결과값을 반환한 결과를 result(extract의 첫번째 매개변수(배열)의 index에 해당하는 곳에 저장함)
}

아무 타입이나 저장할 수 있는 값을 추출하기 위해(표준 any_cast의 파라미터 타입으로 func_type) 전달하고 함수의 인자로 행렬(m)=>여기서는 extract 함수로 전달되는 matrix(템플릿 클래스의 인스턴스)
using func_type = std::tuple_element_t<INDEX, FuncTypes>;

auto& f = std::any_cast<const

result[INDEX] = eval_function<ArgType>(f, s, args...);

template<typename... FuncTypes>
auto set_members(FuncTypes&&... functions)
{
m_data = container_t{ std::any{ };

using return_t = std::tuple< >;

return return_t{};
}

m.set_members(f1, f2, f3, f4, f5, f6); //람다 함수 6개를 matrix 클래스의 메서드인 set_members 의 가변 인자(functions)로 전달하면서 호출
container_t(람다 함수를 vector로 저장할 있는 컨테이너)에 저장(초기화) 하고 그것을 멤버변수(m_data)에 저장함
표준 tuple에서 전달된 람다 함수에서 flat 타입(const volatile reference 제거) 으로 변환하여 저장하고 저장된 result_t는 타입이므로 {}를 감싸서 인스턴스화 하여 반환하고
호출하는곳에서 key로 받아서 그 key는 인스턴스 이므로 decltype으로 형을 알아내고
matrix의 eval 함수에 함수의 독립변수에 원하는 값을 전달하여 최종 6개 람다의 각각의 계산된 6개의 반환값을 출력함
이정도로 이해했는데 잘못된 부분이나 수정사항이 있는 지 알려주시면 감사하겠습니다, 유익한 영상 다시 한번 감사드립니다.

jaeohlee
Автор

Thank you!
What a clever way to deal with matrix with modern cpp!

dw
welcome to shbcf.ru