Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
None
×
Sponsors:
Made by
Andreas Fertig
Source:
#include <type_traits> #include <vector> #include <stdexcept> template <typename T> class Stack { public: void push(T element) requires std::is_scalar_v<T> { elements.push_back(element); } void push(const T& element) requires (!std::is_scalar_v<T>) { elements.push_back(element); } void pop() { if (elements.empty()) throw std::out_of_range("Empty stack"); elements.pop_back(); } const T& top() const { if (elements.empty()) throw std::out_of_range("Empty stack"); return elements.back(); } bool empty() const { return elements.empty(); } private: std::vector<T> elements; }; int main() { Stack<int> si; si.push(42); Stack<std::string> ss; ss.push("hello"); }
Insight:
Console: