Skip to content

Instantly share code, notes, and snippets.

@komori-n
Created Nov 4, 2020
Embed
What would you like to do?
#include <iostream>
#include <cstdlib>
#include <utility>
template <typename T>
struct Hoge {
template <typename U=T>
auto func(void)
-> std::enable_if_t<std::is_same<U, void>::value, void> {
std::cout << "void" << std::endl;
}
template <typename U=T>
auto func(void)
-> std::enable_if_t<!std::is_same<U, void>::value, void> {
std::cout << "not void" << std::endl;
}
};
int main(int argc, char* argv[]) {
Hoge<int>().func();
Hoge<void>().func();
Hoge<void>().func<int>(); // not void
return EXIT_SUCCESS;
}
#include <iostream>
#include <cstdlib>
#include <utility>
template <typename T>
struct Hoge {
template <bool AlwaysTrue = true>
auto func(void)
-> std::enable_if_t<std::is_same<T, void>::value && AlwaysTrue, void> {
std::cout << "void" << std::endl;
}
template <bool AlwaysTrue = true>
auto func(void)
-> std::enable_if_t<!std::is_same<T, void>::value && AlwaysTrue, void> {
std::cout << "not void" << std::endl;
}
};
int main(int argc, char* argv[]) {
Hoge<int>().func();
Hoge<void>().func();
// error!
// Hoge<void>().func<false>();
return EXIT_SUCCESS;
}
#include <iostream>
#include <cstdlib>
#include <utility>
template <typename T>
struct Hoge {
template <std::nullptr_t Dummy = nullptr>
auto func(void)
-> std::enable_if_t<std::is_same<T, void>::value && Dummy == nullptr, void> {
std::cout << "void" << std::endl;
}
template <std::nullptr_t Dummy = nullptr>
auto func(void)
-> std::enable_if_t<!std::is_same<T, void>::value && Dummy == nullptr, void> {
std::cout << "not void" << std::endl;
}
};
int main(int argc, char* argv[]) {
Hoge<int>().func();
Hoge<void>().func();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment