Skip to content

Instantly share code, notes, and snippets.

@komori-n
Created November 4, 2020 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komori-n/086c8d369fbfde0f06e947696c6d11ca to your computer and use it in GitHub Desktop.
Save komori-n/086c8d369fbfde0f06e947696c6d11ca to your computer and use it in GitHub Desktop.
#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