Как обрабатывать ошибки компиляции, связанные с шаблономC++

Программы на C++. Форум разработчиков
Anonymous
Как обрабатывать ошибки компиляции, связанные с шаблоном

Сообщение Anonymous »


Recently I met a C2908 error:

explicit specialization xxx has already been instantiated

The reason is that, for our MyClient class, we have the operator less specifically defined as to compare using the member property ClientSerialNumber; after a wrong header file included, the C2908 error appears.

// Specialization of the Standard C++ predicate less. It is critical to have // it for correct comparison of objects of Myclient class template struct less { bool operator()(const Myclient& lhs, const Myclient& rhs) const { return lhs.ClientSerialNumber < rhs.ClientSerialNumber; } }; In normal compilation error, the line of error shows a type mismatch or a grammar error. The compilation of templates are hidden behind the hundreds of #include. Such error is quite sneaky -- from the compilation error, I only knew semantically what leads to the error, but could not find which line introduced it easily. The error was introduced by a complex merge, in the end I caught the culprit -- a #include line which works in a branch but conflicts after merging -- by adding files one by one.

Honestly I don't know exactly how the error happened yet. We use MSVS, always have the #pragma once protection at the beginning of each header file.

Any suggestions on how to debug, fix, or even better, prevent such template related compilation errors please?


Источник: https://stackoverflow.com/questions/781 ... ion-errors

Вернуться в «C++»