Skip to content

Instantly share code, notes, and snippets.

Avatar
🐢
on going

yoh yohhoy

🐢
on going
View GitHub Profile
@yohhoy
yohhoy / chrono_io.hpp
Last active Dec 15, 2021
Ad-hoc std::ostream support for <chrono>
View chrono_io.hpp
#include <ctime>
#include <chrono>
#include <iostream>
#include <iomanip>
namespace std {
// https://timsong-cpp.github.io/cppwp/n4868/time.cal.ymd.nonmembers#14
// "yyyy-mm-dd"
inline ostream& operator<<(ostream& os, const chrono::year_month_day& ymd) {
@yohhoy
yohhoy / endian.hpp
Last active Oct 28, 2021
endian conversion
View endian.hpp
// requires C++23 or later
#include <bit>
#include <concepts>
template <std::integral T>
constexpr T hton(T value) noexcept
{
if constexpr (std::endian::native == std::endian::little) {
return std::byteswap(value);
} else {
@yohhoy
yohhoy / cv2ff.cpp
Created Aug 4, 2021
Convert from OpenCV image and write movie with FFmpeg (OpenCV 4.5, FFmpeg 4.4)
View cv2ff.cpp
/*
* Convert from OpenCV image and write movie with FFmpeg
*
* Copyright (c) 2021 yohhoy
*/
#include <iostream>
#include <vector>
// FFmpeg
extern "C" {
#include <libavformat/avformat.h>
View pre-p2210r2.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <ranges>
int main() {
std::string_view str = "apple,banana,cinnamon";
for (auto rev_item: str | std::views::reverse | std::views::split(',')) {
static_assert( std::ranges::forward_range<decltype(rev_item)>);
View error-concepts-diagnostics.log
prog.cc: In function 'int main()':
prog.cc:6:19: error: no match for 'operator|' (operand types are 'std::forward_list<int, std::allocator<int> >' and 'const std::ranges::views::_Reverse')
6 | for (int _: lst | std::views::reverse) {}
| ~~~ ^ ~~~~~~~~~~~~~~~~~~~
| | |
| | const std::ranges::views::_Reverse
| std::forward_list<int, std::allocator<int> >
In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/12.0.0/ranges:818:7: note: candidate: 'template<class _Lhs, class _Rhs> requires (derived_from<_Lhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) && (derived_from<_Rhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) constexpr auto std::ranges::views::__adaptor::operator|(_Lhs, _Rhs)'
818 | operator|(_Lhs __lhs, _Rhs __rhs)
@yohhoy
yohhoy / mock-string.cpp
Last active Jul 2, 2021
Mock class template
View mock-string.cpp
#include <initializer_list>
namespace mock {
template <class T>
struct allocator {};
template <class T>
struct char_traits {};
// C++20 https://timsong-cpp.github.io/cppwp/n4861/basic.string
template<class charT, class traits = char_traits<charT>,
View utf8_util.hpp
bool starts_with_utf8bom(std::string_view s)
{
constexpr char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
return s.length() >= 3 && std::equal(utf8_bom, utf8_bom + 3, s.begin());
}
std::string& erase_utf8bom(std::string& text)
{
if (starts_with_utf8bom(text)) {
text.erase(0, 3);
View decltype.cpp
#include <type_traits>
int main() {
int x = 42;
static_assert(std::is_same_v<decltype( x ), int>);
static_assert(std::is_same_v<decltype((x)), int&>);
int (y) = 42; // redundant parentheses
static_assert(std::is_same_v<decltype( y ), int>);
static_assert(std::is_same_v<decltype((y)), int&>);
@yohhoy
yohhoy / spdlog-templ.cpp
Last active Jul 15, 2021
template code for spdlog library
View spdlog-templ.cpp
#include <memory>
#include <vector>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
int main(int argc, char* argv[])
{
@yohhoy
yohhoy / same_as.cpp
Last active Oct 17, 2019
same_as concept in C++20 Standard Library
View same_as.cpp
//
// requires Micosoft VisualC++ 2019 16.3 Preview 2 that implements P0717R1.
//
// https://devblogs.microsoft.com/cppblog/c20-concepts-are-here-in-visual-studio-2019-version-16-3/
// https://wg21.link/p0717r1 Semantic constraint matching for concepts
#include <type_traits>
#if 1
template <class T, class U>
concept same_as_impl = std::is_same_v<T, U>;