Napisałem kiedyś jakiś kod, który wygenerował statyczną tablicę/tablicę w czasie kompilacji dla metaprogramowania niektórych szablonów (chodzi o to, że łańcuchy w stylu C mogą być budowane w czasie kompilacji (są to tylko tablice char
)). Pomysł i kod opiera się David Lin „s answer:Generowanie tabel statycznych działa z GCC, ale nie klang; czy błąd jest clang?
#include <iostream>
const int ARRAY_SIZE = 5;
template <int N, int I=N-1>
class Table : public Table<N, I-1>
{
public:
static const int dummy;
};
template <int N>
class Table<N, 0>
{
public:
static const int dummy;
static int array[N];
};
template <int N, int I>
const int Table<N, I>::dummy = Table<N, 0>::array[I] = I*I + 0*Table<N, I-1>::dummy;
template <int N>
int Table<N, 0>::array[N];
template class Table<ARRAY_SIZE>;
int main(int, char**)
{
const int *compilerFilledArray = Table<ARRAY_SIZE>::array;
for (int i=0; i < ARRAY_SIZE; ++i)
std::cout<<compilerFilledArray[i]<<std::endl;
}
Kompilacja tego kodu z GCC 4.9.2 działa:
Clang 3.5 narzeka, ale:
$ clang++ -Wall -pedantic b.cpp
Undefined symbols for architecture x86_64:
"Table<5, 0>::dummy", referenced from:
___cxx_global_var_init in b-b8a447.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
dummy
i array
mają podane definicje poza klasą Table
(gdzie są zadeklarowane). O ile mogę powiedzieć, to powinno spełniać wymagania linkera.
Czy to błąd z dzwonkiem?
Ach, to ma sens! Szkoda, że mogę głosować tylko raz. Poza tym czekam na standardowe cytaty, kiedy je otrzymasz. – Cornstalks
@Cornstalks Oni tam są :) – Columbo
Czy możesz dodać link do raportu o błędzie gcc, proszę? –