fork download
  1. #include <type_traits>
  2. #include <iostream>
  3. struct A {
  4. static constexpr unsigned int bar = 20;
  5.  
  6. };
  7.  
  8. struct B {
  9.  
  10. };
  11.  
  12.  
  13. template <typename T,typename=void>
  14. struct getBar {
  15. static constexpr unsigned int bar = 0;
  16. };
  17.  
  18. template <typename T>
  19. struct getBar<T,std::void_t<decltype(T::bar)>> {
  20. static constexpr unsigned int bar = T::bar;
  21. };
  22.  
  23. int main() {
  24. std::cout << getBar<A>::bar << std::endl; // Expect 20
  25. std::cout << getBar<B>::bar << std::endl; //Expect 0
  26. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
20
0