Skip to content

Instantly share code, notes, and snippets.

@semmel
Last active November 21, 2016 20:57
Show Gist options
  • Save semmel/28455fc715f71805d92e3d9e2f0677ed to your computer and use it in GitHub Desktop.
Save semmel/28455fc715f71805d92e3d9e2f0677ed to your computer and use it in GitHub Desktop.
Gist created by fiddle.jyt.io
// Jyt is a REPL for C++
// You can write code interactively
// Highlight some code and press alt-enter to execute it
// For example:
auto x = "hello world";
// Now you can query the value in the terminal on the right
// e.g. "x"
// You can also update the value
// x = "hello again";
// The terminal on the right is meant for evaluating expressions.
// This window is on the otherhand for definitions. E.g.:
int foo(int x) {
return x;
}
struct Confi
{
bool isMale;
bool isOutgoing;
Confi(): isMale(false), isOutgoing(false)
{}
Confi(bool isMale_, bool isOutgoing_): isMale(isMale_), isOutgoing(isOutgoing_)
{}
};
// And you can include libraries
#include<iostream>
#include <functional>
#include <memory>
#include <map>
#include <unordered_map>
#include <algorithm>
void custom_delete(int* p)
{
std::cout << "custom_delete";
delete p;
}
void no_delete(int* p)
{
std::cout << "no_delete";
}
typedef std::unique_ptr<int, std::function<void(int*)>> UniqueIntPtr;
typedef std::shared_ptr<int> SharedIntPtr;
UniqueIntPtr uniqueNumberPtr(new int, custom_delete);
SharedIntPtr sharedNumberPtr(new int, custom_delete);
int* pI = new int;
// Press the "play" button to load this file and interact in the terminal.
// As C++ does not allow to redefine symbols, pressing "play" multiple times might cause errors.
// You can restart the whole enviroment by pressing the "reload" button.
std::map<SharedIntPtr, int> numberByPtr;
std::unordered_map<int, Confi> confiByNr;
int main(std::string x) {
std::cout << x << " " << foo(42) << std::endl;
*uniqueNumberPtr = 4;
std::cout << *uniqueNumberPtr;
*sharedNumberPtr = 5;
numberByPtr.insert({sharedNumberPtr, 5});
*pI = 9;
numberByPtr.insert({SharedIntPtr(pI, custom_delete), 9});
return 0;
}
template <typename T>
void printCollValues(const T& coll)
{
std::for_each(coll.cbegin(), coll.cend(), [](auto pair){ std::cout << pair.second << std::endl; });
}
int binAdd(int a, int b){ return a + b; }
template <typename... Types>
int sum(const Types... summands)
{
return binAdd(summands...);
}
auto result = main(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment