Skip to content

Instantly share code, notes, and snippets.

@cdacamar
Created July 16, 2021 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdacamar/8226c1893dd8c8a81974c939be52809f to your computer and use it in GitHub Desktop.
Save cdacamar/8226c1893dd8c8a81974c939be52809f to your computer and use it in GitHub Desktop.
More sophisticated modules demo
#include <iostream>
import shop;
int main() {
auto bag = shop();
if (bag.products.size() != 2) {
std::cout << "Bag did not have the expected size!\n";
return 1;
}
if (bag.products[0].value != "fruit") {
std::cout << "First product is not a fruit!\n";
}
if (bag.products[1].value != "veggie") {
std::cout << "First product is not a veggie!\n";
}
std::cout << "Shopping bag had everything we expected!\n";
}
module;
#include <memory>
module shop;
import util;
ShoppingBag shop() {
ShoppingBag bag;
bag.products.push_back(produce("fruit"));
bag.products.push_back(produce("veggie"));
return bag;
}
export module shop;
import types;
export
ShoppingBag shop();
module;
#include <string>
#include <vector>
export module types;
export
struct Product {
std::string value;
};
export
struct ShoppingBag {
std::vector<Product> products;
};
module;
#include <string>
export module util;
import types;
export
Product produce(const char* product) { return { .value = product }; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment