Erwin Unruh的例子今天有用吗?

时间:2018-02-18 07:33:27

标签: c++ g++ template-meta-programming

以下是来自Erwin Unruh的{​​{3}},因使用C++模板元编程在编译器错误消息中生成素数而闻名,如下所示。

// Erwin Unruh, untitled program, 
// ANSI X3J16-94-0075/ISO WG21-462, 1994.

template <int i>
struct D
{
    D(void *);
    operator int();
};

template <int p, int i>
struct is_prime
{
    enum { prim = (p%i) && is_prime<(i>2?p:0), i>::prim };
};

template <int i>
struct Prime_print
{
    Prime_print<i-1>    a;
    enum { prim = is_prime<i,i-1>::prim };
    void f() { D<i> d = prim; }
};

struct is_prime<0,0> { enum { prim = 1 }; };
struct is_prime<0,1> { enum { prim = 1 }; };
struct Prime_print<2>
{
    enum { prim = 1 };
    void f() { D<2> d = prim; }
};

void foo()
{
    Prime_print<10> a;
}

结果应该是此编译器错误消息:

// output:
// unruh.cpp 30: conversion from enum to D<2> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<3> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<5> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<7> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<11> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<13> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<17> requested in Prime_print
// unruh.cpp 30: conversion from enum to D<19> requested in Prime_print

我检查了这段代码,并通过g++进行了编译,并没有得到相同的结果。

尽管我通过

使用了c++17
g++ -std=c++17 -O3 main.cpp

似乎即使c++03也不起作用。结果与预期的结果完全不同。我应该使用不同的编译器选项吗?

main.cpp:25:1: error: an explicit specialization must be preceded by ‘template <>’
 struct is_prime<0,0> { enum { prim = 1 }; };
 ^~~~~~~~~~~~~~~~~~~~
 template <> 
main.cpp:26:1: error: an explicit specialization must be preceded by ‘template <>’
 struct is_prime<0,1> { enum { prim = 1 }; };
 ^~~~~~~~~~~~~~~~~~~~
 template <> 
main.cpp:27:1: error: an explicit specialization must be preceded by ‘template <>’
 struct Prime_print<2>
 ^~~~~~~~~~~~~~~~~~~~~
 template <> 
main.cpp: In member function ‘void Prime_print<2>::f()’:
main.cpp:30:25: error: conversion from ‘Prime_print<2>::<unnamed enum>’ to non-scalar type ‘D<2>’ requested
     void f() { D<2> d = prim; }
                         ^~~~
main.cpp: In instantiation of ‘struct is_prime<0, 2>’:
main.cpp:14:25:   required from ‘struct is_prime<3, 2>’
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<0, 2>’ used in nested name specifier
     enum { prim = (p%i) && is_prime<(i>2?p:0), i>::prim };
                   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In instantiation of ‘struct is_prime<4, 3>’:
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<4, 3>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<5, 4>’:
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<5, 4>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<6, 5>’:
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<6, 5>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<7, 6>’:
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<7, 6>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<8, 7>’:
main.cpp:20:25:   recursively required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<8, 7>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<9, 8>’:
main.cpp:21:10:   required from ‘struct Prime_print<9>’
main.cpp:20:25:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<9, 8>’ used in nested name specifier
main.cpp: In instantiation of ‘struct is_prime<10, 9>’:
main.cpp:21:10:   required from ‘struct Prime_print<10>’
main.cpp:35:21:   required from here
main.cpp:14:25: error: incomplete type ‘is_prime<10, 9>’ used in nested name specifier

更新1

用户Tony Delroy建议使用example。我尝试过这段代码而且它也没有用。

template <int i>
struct D
{
    D(void *);
    operator int();
};

template <int p, int i>
struct is_prime
{
    enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1>::prim };
};

template <int i>
struct Prime_print
{
    Prime_print<i-1>    a;
    enum { prim = is_prime<i,i-1>::prim };
    void f() { D<i> d = prim ? 1 : 0; a.f(); }
};

struct is_prime<0,0> { enum { prim = 1 }; };
struct is_prime<0,1> { enum { prim = 1 }; };

struct Prime_print<2>
{
    enum { prim = 0 };
    void f() { D<1> d = prim? 1 : 0; }
};

int main()
{
    Prime_print<18> a;
    a.f()
}

更新2

我找到了一个有效的代码alternative code。 唯一的问题是它应与grep级联以提供结果

g++ -std=c++03 -c -fpermissive main.cpp 2>&1 | grep "In instantiation"

输出:

main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 17]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 13]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 11]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 7]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 5]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 3]’:
main.cpp: In instantiation of ‘void Prime_print<i>::f() [with int i = 2]’:

有没有办法在不使用Linux grep命令的情况下产生单行错误?

/*
  Prime number computation by Erwin Unruh
  http://www.erwin-unruh.de/Prim.html
  compatible with (now!) "valid" C++
  compile --> the error messages are the prime numbers up to 18!
  for clang: c++ unruh_new.cpp 2>&1 | grep -i error
  for gnu:   g++-mp-5 -std=c++03 -c                                \
                      -fpermissive                                 \
                      unruh_new.cpp 2>&1 | grep "In instantiation"
*/

template <int i> struct D { D(void*); operator int(); };

template <int p, int i> struct is_prime {
 enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1> :: prim };
};

template <int i> struct Prime_print {
 Prime_print<i-1> a;
 enum { prim = is_prime<i, i-1>::prim };
 void f() { D<i> d = prim ? 1 : 0; a.f();}
};

template<> struct is_prime<0,0> { enum {prim=1}; };
template<> struct is_prime<0,1> { enum {prim=1}; };

template<> struct Prime_print<1> {
 enum {prim=0};
 void f() { D<1> d = prim ? 1 : 0; };
};

#ifndef LAST
#define LAST 18
#endif

main() {
 Prime_print<LAST> a;
 a.f();
}

2 个答案:

答案 0 :(得分:1)

在线搜索,“发现现代C ++”一书的a preview列出了<Provider store={this.store}>中略有不同的源代码:

struct is_prime

下一页列出了很多错误,例如你记录的错误,以及显示编译器输出的页面,更简洁地列出了素数:

enum { prim = (p == 2) || (p%i) etc...

所以 - 我建议更仔细地查看您的错误消息。但是,如何报告错误取决于您的编译器,并且可以合理地随版本而变化,因此编译器不能产生您期望的错误。

答案 1 :(得分:1)

在下面,您会找到我在C ++讲座中使用的更新程序。即使使用g ++-8 --std = c ++ 2a,它也可以完美运行(即会产生错误;)。要尝试一下,我建议使用以下脚本:不带参数的质数直到25,带数字参数(例如100)的质数直到100。通过sed过滤输出,显示重要行。如果您希望所有编译器的输出都提供任何第二个参数。

这是脚本:

#!/bin/bash

if [ $# -eq 0 ] ; then
    last=25
else
    last=$1
fi

if [ $# -gt 1 ] ; then
   g++ --std=c++2a -DLAST=${last} primefrank.cc |& less
else
    (g++ --std=c++2a -DLAST=${last} primefrank.cc 2>&1) |
                                   grep 'instantiation of' |
                                   sed 's/ instantiation of//'
fi

这是程序表的来源:

#ifndef LAST
#define LAST 18
#endif

enum 
{
    IS_PRIME,
    NO_PRIME,
    CONTINUE  
};

template <int candidate, int testValue>
struct Eval
{
    enum
    { 
        mode = 
            testValue * testValue > candidate ? IS_PRIME  :    
            candidate % testValue == 0 ?        NO_PRIME  :
                                                CONTINUE
    };
};

template <int candidate, int prime, int mode >
struct sieve
{
    enum 
    { 
        next = prime + 1,
        isPrime = sieve<candidate, next, 
                        Eval<candidate, next>::mode>::isPrime
    };
};

template <int candidate, int prime> 
struct sieve<candidate, prime, IS_PRIME>
{
    enum { isPrime = IS_PRIME };
};

template <int candidate, int prime>
struct sieve<candidate, prime, NO_PRIME >
{
    enum { isPrime = NO_PRIME };
};

template <int prime>
struct test
{
    enum {isPrime = sieve<prime, 2, Eval<prime, 2>::mode>::isPrime };
};

template <int prime, int isPrime>
struct show
{                           
    static void f()
    {
        show<prime - 1, test<prime - 1>::isPrime >::f();
    }
};

template <int prime>
struct show<prime, IS_PRIME>
{
    static int *f()
    {
        show<prime - 1, test<prime - 1>::isPrime >::f();

        int x;
        return &x;
    }
};

template <>
struct show<1, IS_PRIME>
{
    static void f()
    {}
};

template <int prime>
void primes()
{
    show<prime, test<prime>::isPrime>::f();
}

int main() 
{
    // 'instantiation' messages because of the suggested grep command
    static_assert(LAST >= 2, 
        "instantiation of LAST must be >= 2");

    primes<LAST>();

    static_assert(0, 
        "instantiation of compilation terminated");
}