-
-
Save cdacamar/8226c1893dd8c8a81974c939be52809f to your computer and use it in GitHub Desktop.
More sophisticated modules demo
This file contains 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 <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"; | |
} |
This file contains 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
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; | |
} |
This file contains 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
export module shop; | |
import types; | |
export | |
ShoppingBag shop(); |
This file contains 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
module; | |
#include <string> | |
#include <vector> | |
export module types; | |
export | |
struct Product { | |
std::string value; | |
}; | |
export | |
struct ShoppingBag { | |
std::vector<Product> products; | |
}; |
This file contains 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
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