tinycodinglm / cpp /cpp-modules-cpp20.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.78 kB
opencode trace
System You are an expert C++ developer. You write clean, efficient, and idiomatic code.

You are an expert C++ developer. You write clean, efficient, and idiomatic code.

User

Use C++20 modules for faster compilation and better encapsulation

Assistant

// Module interface (math.cppm)
// export module math;
//
// export namespace math {
// int add(int a, int b) { return a + b; }
// int multiply(int a, int b) { return a * b; }
// }

// Module usage (main.cpp)
// import math;
// int main() {
// return math::add(1, 2);
// }

// Header units example
// import <iostream>;
// import <vector>;
// import <string>;

// Below shows the module interface pattern
#include <iostream>
#include <vector>
#include <string>

// Simulated module interface
export module example;

export namespace example {
int add(int a, int b) { return a + b; }

markdown
class Calculator {
public:
    int add(int a, int b) const { return a + b; }
    int subtract(int a, int b) const { return a - b; }
    double divide(double a, double b) const { return a / b; }
};

}

// Module partition
export module example:utils;

export namespace example::utils {
std::string toUpper(const std::string& s) {
std::string result = s;
for (auto& c : result) c = toupper(c);
return result;
}
}

int main() {
example::Calculator calc;
std::cout << calc.add(3, 4) << "\n";
std::cout << example::utils::toUpper("hello") << "\n";
return 0;
}