Last active
January 30, 2023 12:13
-
-
Save doevelopper/968b4ff6c9bcdf97975d0d408333fd64 to your computer and use it in GitHub Desktop.
Commom cpp timps found on net :)
template <class Key, class Value>
struct Foo {};
template <class Key>
struct Foo<Key, int> {};
template <>
struct Foo<std::string, int> {};
Then, when instantiating the templates, the compiler will choose the most specialized definition available:
Foo<std::string, std::string> f1; // Should match #1
Foo<int, int> f2; // Should match #2
Foo<std::string, int> f3; // Should match #3
#1 and #3 work for template functions as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```cpp | |
#include <iostream> | |
#include <random> | |
#include <cstdlib> | |
struct ContextTag | |
{ | |
ContextTag() { std::cout << "ContextTag::ContextTag" << std::endl;} | |
~ContextTag() { std::cout << "~ContextTag::ContextTag" << std::endl;} | |
}; | |
struct ThreadContextTag | |
{ | |
ThreadContextTag() { std::cout << "ThreadContextTag::ThreadContextTag" << std::endl;} | |
~ThreadContextTag() { std::cout << "~ThreadContextTag::ThreadContextTag" << std::endl;} | |
}; | |
struct CoroContextTag | |
{ | |
CoroContextTag() { std::cout << "CoroContextTag::CoroContextTag" << std::endl;} | |
~CoroContextTag() { std::cout << "~CoroContextTag::CoroContextTag" << std::endl;} | |
}; | |
template <typename T> | |
struct TagTrait | |
{ | |
TagTrait() { std::cout << "TagTrait::TagTrait" << std::endl;} | |
~TagTrait() { std::cout << "~TagTrait::TagTrait" << std::endl;} | |
}; | |
template <> | |
struct TagTrait<long> | |
{ | |
using type = ContextTag; | |
}; | |
template <> | |
struct TagTrait<int> | |
{ | |
using type = ThreadContextTag; | |
}; | |
template <> | |
struct TagTrait<char> | |
{ | |
using type = CoroContextTag; | |
}; | |
template <typename T> | |
using TagTrait_t = typename TagTrait<T>::type; | |
template <typename T> | |
void f(T t, ContextTag) | |
{ | |
std::cout << "ContextTag::" << t << std::endl; | |
} | |
template <typename T> | |
void f(T t, ThreadContextTag) | |
{ | |
std::cout << "ThreadContextTag::" << t << std::endl; | |
} | |
template <typename T> | |
void f(T t, CoroContextTag) | |
{ | |
std::cout << "CoroContextTag::" << t << std::endl; | |
} | |
template <typename T> | |
void f(T t) | |
{ | |
f(t, TagTrait_t<T>{}); | |
} | |
int | |
main() | |
{ | |
f(01); // uses f(..., ContextTag) | |
f(0); // uses f(..., ThreadContextTag) | |
//f('0'); // uses f(..., CoroContextTag) ??? | |
return (EXIT_SUCCESS); | |
} | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
// Inspired by / taken from: | |
// https://stackoverflow.com/questions/11328264/python-like-loop-enumeration-in-c | |
// https://www.reedbeta.com/blog/python-like-enumerate-in-cpp17/ | |
template<typename Iterable> | |
auto enumerate(Iterable&& iterable) | |
{ | |
using Iterator = decltype(std::begin(std::declval<Iterable>())); | |
using T = decltype(*std::declval<Iterator>()); | |
struct Enumerated | |
{ | |
std::size_t index; | |
T element; | |
}; | |
struct Enumerator | |
{ | |
Iterator iterator; | |
std::size_t index; | |
auto operator!=(const Enumerator& other) const | |
{ | |
return iterator != other.iterator; | |
} | |
auto& operator++() | |
{ | |
++iterator; | |
++index; | |
return *this; | |
} | |
auto operator*() const | |
{ | |
return Enumerated{index, *iterator}; | |
} | |
}; | |
struct Wrapper | |
{ | |
Iterable& iterable; | |
[[nodiscard]] auto begin() const | |
{ | |
return Enumerator{std::begin(iterable), 0U}; | |
} | |
[[nodiscard]] auto end() const | |
{ | |
return Enumerator{std::end(iterable), 0U}; | |
} | |
}; | |
return Wrapper{std::forward<Iterable>(iterable)}; | |
} | |
//----------------------------------------------------------------------------------------------------- | |
int main() | |
{ | |
std::vector<int> data{12, 89, 654, 123, 4587, 0}; | |
for (auto&& enumerated: enumerate(data)) | |
{ | |
std::cout << "index = " << enumerated.index << " element = " << enumerated.element << '\n'; | |
} | |
std::cout << "*----------------------" << '\n'; | |
for (auto&& enumerated: enumerate(data)) | |
{ | |
enumerated.element *= 2; | |
} | |
std::cout << "*----------------------" << '\n'; | |
for (auto&& enumerated: enumerate(data)) | |
{ | |
std::cout << "index = " << enumerated.index << " element = " << enumerated.element << '\n'; | |
} | |
std::cout << "*----------------------" << '\n'; | |
// The following requires C++17 as it uses structured binding | |
for (auto&&[index, character]: enumerate("bonjour")) | |
{ | |
std::cout << "index = " << index << " element = " << character << '\n'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```txt | |
https://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Writing_efficient_code/Constructions_and_destructions | |
``` | |
#include <boost/shared_ptr.hpp> | |
#include <fstream> | |
class urandom32 | |
{ | |
boost::shared_ptr<std::ifstream> device; | |
public: | |
urandom32() : device(new std::ifstream("/dev/urandom")) { } | |
uint32_t operator()() | |
{ | |
uint32_t r; | |
device->read(reinterpret_cast<char *>(&r), sizeof(uint32_t)); | |
return r; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cxxabi.h> | |
#include <iostream> | |
#include <typeinfo> | |
// http://en.cppreference.com/w/cpp/types/type_info/name | |
// https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html | |
template<typename T> | |
void sayMyName() { | |
auto name = typeid(T).name(); | |
std::cout << "Raw: " << name << '\n'; | |
std::cout << "Demangled: " << abi::__cxa_demangle(name, nullptr, nullptr, nullptr) << '\n' << '\n'; | |
} | |
class Foo { | |
}; | |
using Callback = std::pair<int, int>(*)(const Foo&); | |
int main() { | |
sayMyName<char>(); | |
sayMyName<signed short>(); | |
sayMyName<int>(); | |
sayMyName<unsigned long long>(); | |
sayMyName<float>(); | |
sayMyName<double>(); | |
sayMyName<Foo>(); | |
sayMyName<Callback>(); | |
} |
#include <iostream>
#include <cstdlib>
#include <UnitTestBuilder.hpp>
int
main(
[[maybe_unused]] int argc,
[[maybe_unused]] char ** argv)
{
std::uint8_t runStatus = 0;
try
{
UnitTestBuilder unit_test;
runStatus = unit_test.run(argc,argv);
}
catch ( const std::exception & e )
{
std::cerr << e.what ( ) << std::endl;
return ( EXIT_FAILURE );
}
catch ( ... )
{
std::cerr << "unknown exception\n";
return ( EXIT_FAILURE );
}
if (runStatus != EXIT_SUCCESS)
{
std::cerr << "FAILED!" << std::endl;
}
else
{
std::cout << "Hello, World!" << std::endl;
}
return (EXIT_SUCCESS);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment