Skip to content

Comment on Solving FizzBuzz with C++ compiler error messages

Comments

How about a solution for primes below a number using Sieve of Eratosthenes:

  template<int H=-1, typename T=void>
  struct tl {
      enum {head=H};
      typedef T tail;
  };
  
  template<>
  struct tl<> {
  };
  
  template<int N, typename Then, typename Else>
  struct if_c {
      typedef Then type;
  };
  
  template<typename Then, typename Else>
  struct if_c<false, Then, Else> {
      typedef Else type;
  };
  
  template<int N, typename SoFar>
  struct is_prime {
      enum {value = N%SoFar::head && is_prime<N, SoFar::tail>::value};
  };
  
  template<int N>
  struct is_prime<N, tl<>> {
      enum {value = 1};
  };
  
  template<int N, int C, typename SoFar=tl<>>
  struct prime {
      typedef typename prime<N+1, C-1, typename if_c<is_prime<N, SoFar>::value, tl<N, SoFar>, SoFar>::type>::type type;
  };
  
  template<int N, typename SoFar>
  struct prime<N, 0, SoFar> {
      typedef SoFar type;
  };
  
  template<int N>
  struct number : prime<2, N-2> {
  };
  
  template<>
  struct number<0> {
  };
  
  template<>
  struct number<1> {
  };
  
  template<typename T>
  struct print {
  };
  
  void main() {
      typedef print<number<300>::type>::type type;
  }

Here is result from Visual Studio 2005:
  c:\console.cpp(113) : error C2039: 'type' : is not a member of 'print<T>' with [T=tl<293,tl<283,tl<281,tl<277,tl<271,tl<269,tl<263,tl<257,tl<251,tl<241,tl<239,tl<233,tl<229,tl<227,tl<223,tl<211,tl<199,tl<197,tl<193,tl<191,tl<181,tl<179,tl<173,tl<167,tl<163,tl<157,tl<151,tl<149,tl<139,tl<137,tl<131,tl<127,tl<113,tl<109,tl<107,tl<103,tl<101,tl<97,tl<89,tl<83,tl<79,tl<73,tl<71,tl<67,tl<61,tl<59,tl<53,tl<47,tl<43,tl<41,tl<37,tl<31,tl<29,tl<23,tl<19,tl<17,tl<13,tl<11,tl<7,tl<5,tl<3,tl<2,tl<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]

Couldn't help but notice you're one of those void main people..

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.