Skip to content

Instantly share code, notes, and snippets.

View debbynirwan's full-sized avatar
🏠
Working from home

Debby Nirwan debbynirwan

🏠
Working from home
View GitHub Profile
class PacmanAlgo : public AgentImpl {
PacmanAlgo()
: AgentImpl(true) {}
virtual std::variant<Direction, Stop> GetAction(const WorldState& worldState,
const std::size_t id) override
{
return Stop{};
}
};
template<typename T>
T Square(const T& t) {
if constexpr (std::is_arithmetic<T>::value) {
return t * t;
} else {
return t.value * t.value;
}
}
int Square(const int& t) {
if (true) {
return t * t;
} else {
return t.value * t.value;
}
}
template<typename T>
typename std::enable_if<std::is_arithmetic<T>::value, T>::type Square(const T& t) {
return t * t;
}
template<typename T>
typename std::enable_if<! std::is_arithmetic<T>::value, T>::type Square(const T& t) {
return t.value * t.value;
}
template<typename T>
T Square(const T& t) {
if (std::is_arithmetic<T>::value) {
return t * t;
} else {
return t.value * t.value;
}
}
int integer_num = 5;
float floating_num = 5.0;
bool boolean = true;
Number<int> number_int(5);
auto res = Square(integer_num); // call int Square(int);
auto res2 = Square(floating_num); // call float Square(float);
auto res3 = Square(boolean); // call bool Square(bool);
auto res4 = Square(number_int); // call Number<int> Square(Number<int>);
// failed to compile, operator* not found
template<typename T>
T Square(const T& t) {
return t * t;
}
template <typename T>
struct Number {
Number(const T& _val) :
value(_val) {}
T value;
};
constexpr int Sum(const int a, const int b) {
return a + b;
}
int main(int argc, char **argv) {
constexpr int val = Sum(1, 2);
int var = 3;
int val2 = Sum(val, var);
return 0;
int main(int argc, char **argv) {
int val = 3;
return 0;
}