|
Download cpp/cpp-constexpr-compute.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/1464c23364f23349d4bfdab40fcdf171491d7c20/cpp/cpp-constexpr-compute.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm@1464c23364f23349d4bfdab40fcdf171491d7c20/cpp/cpp-constexpr-compute.jsonl
-
curl -L -o cpp-constexpr-compute.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/1464c23364f23349d4bfdab40fcdf171491d7c20/cpp/cpp-constexpr-compute.jsonl
2.16 kB
| {"type": "session", "harness": "opencode", "id": "cpp-constexpr-compute", "name": "Use C++ constexpr for compile-time computation and metaprogramming"} | |
| {"type": "message", "message": {"role": "system", "content": "You are an expert C++ developer. You write clean, efficient, and idiomatic code."}} | |
| {"type": "message", "message": {"role": "user", "content": "Use C++ constexpr for compile-time computation and metaprogramming"}} | |
| {"type": "message", "message": {"role": "assistant", "content": "#include <array>\n#include <iostream>\n#include <type_traits>\n\n// Compile-time factorial\nconstexpr uint64_t factorial(int n) {\n return n <= 1 ? 1 : n * factorial(n - 1);\n}\n\n// Compile-time fibonacci\nconstexpr uint64_t fib(int n) {\n if (n <= 1) return n;\n uint64_t a = 0, b = 1;\n for (int i = 2; i <= n; ++i) {\n uint64_t temp = a + b;\n a = b;\n b = temp;\n }\n return b;\n}\n\n// Compile-time string\ntemplate<size_t N>\nstruct ConstString {\n char data[N];\n constexpr ConstString(const char (&str)[N]) {\n for (size_t i = 0; i < N; ++i) data[i] = str[i];\n }\n constexpr char operator[](size_t i) const { return data[i]; }\n constexpr size_t size() const { return N - 1; }\n};\n\n// Compile-time array generation\ntemplate<int N>\nconstexpr auto makeArray() {\n std::array<int, N> arr{};\n for (int i = 0; i < N; ++i) arr[i] = i * i;\n return arr;\n}\n\n// If constexpr\ntemplate<typename T>\nauto process(T value) {\n if constexpr (std::is_integral_v<T>) {\n return value * 2;\n } else if constexpr (std::is_floating_point_v<T>) {\n return value * 1.5;\n } else {\n return value;\n }\n}\n\nint main() {\n constexpr auto fact10 = factorial(10);\n std::cout << \"10! = \" << fact10 << \"\\n\";\n\n constexpr auto fib20 = fib(20);\n std::cout << \"fib(20) = \" << fib20 << \"\\n\";\n\n constexpr auto squares = makeArray<10>();\n for (int i = 0; i < 10; ++i) std::cout << squares[i] << \" \";\n std::cout << \"\\n\";\n\n std::cout << process(42) << \"\\n\";\n std::cout << process(3.14) << \"\\n\";\n\n return 0;\n}"}} | |