मैंने कुछ समय एक कोड लिखा था जिसने कुछ टेम्पलेट मेटाप्रोग्रामिंग के लिए संकलन समय पर स्थिर तालिका/सरणी उत्पन्न की थी (विचार यह है कि सी-शैली तारों को संकलित समय पर बनाया जा सकता है (वे केवल char
सरणी हैं)। विचार और कोड पर आधारित है David Lin के answer:स्टेटिक टेबल पीढ़ी जीसीसी के साथ काम करता है लेकिन झगड़ा नहीं; क्लैंग बग है?
#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;
}
जीसीसी 4.9.2 काम करता है के साथ इस कोड संकलन:
बजना 3.5 शिकायत है, हालांकि:
$ 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
और array
दोनों को Table
कक्षा (जहां उन्हें घोषित किया गया है) के बाहर दी गई परिभाषाएं दी गई हैं। जहां तक मैं कह सकता हूं, यह लिंकर आवश्यकताओं को पूरा करना चाहिए।
क्या यह क्लैंग के साथ एक बग है?
आह, यह समझ में आता है! यह एक शर्म की बात है कि मैं केवल एक बार मतदान कर सकता हूं। साथ ही, जब आप उन्हें प्राप्त करते हैं तो मैं मानक उद्धरणों की प्रतीक्षा कर रहा हूं। – Cornstalks
@ कॉर्नस्टॉक वे वहां हैं :) – Columbo
क्या आप कृपया जीसीसी बग रिपोर्ट में एक लिंक जोड़ सकते हैं? –