Skip to content

Instantly share code, notes, and snippets.

@martok
Created August 31, 2019 20:05
Show Gist options
  • Save martok/3bc302651b318861425897e67319fc18 to your computer and use it in GitHub Desktop.
Save martok/3bc302651b318861425897e67319fc18 to your computer and use it in GitHub Desktop.
Set operation in C++
#include <iostream>
using namespace std;
/*
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
static inline bool set_in(T val, T a1, T a2) { return (val==a1||val==a2); }
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
static inline bool set_in(T val, T a1, T a2, T a3) { return (val==a1||val==a2||val==a3); }
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
static inline bool set_in(T val, T a1, T a2, T a3, T a4) { return (val==a1||val==a2||val==a3||val==a4); }
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
static inline bool set_in(T val, T a1, T a2, T a3, T a4, T a5) { return (val==a1||val==a2||val==a3||val==a4||val==a5); }
*/
typedef enum { FCC, BCC, HCP, DIM, DIA, DIA3, B1, C11, L12, B2, CH4, LIN, ZIG, TRI } lattice_t;
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
constexpr bool set_ain(T val) {
return false;
}
template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr, typename ...Trest>
constexpr bool set_ain(T val, T a1, Trest&& ...rest) {
return (val==a1) || set_ain(val, rest...);
}
// for (int i=0; i<n; i++) if (val==vals[i]) return true; return false; }
int main() {
volatile lattice_t lat = BCC;
if (set_ain(lat, DIA, CH4, DIA3, CH4, B2, LIN, L12, ZIG)) {
std::cout << "set in" << std::endl;
} else {
std::cout << "set not in" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment