そのため、関数テンプレートの部分的な特殊化に問題がありました。ここで説明するソリューションを選択します:質問
今私はこれを持っています:
#include <vector>
#include <iostream>
template <typename T> struct helper {
static void print(T value) { std::cout << value; }
};
template <typename T> struct helper<std::vector<T>> {
static void print(std::vector<T> const &value) { }
};
template <typename T>
void print (T const &value) {
// Just delegate.
helper<T>::print (value);
}
int main () {
print (5);
std::vector<int> v;
print (v);
}
しかし、私はこのレイアウトが欲しいです:
helper.hpp
#include <vector>
#include <iostream>
template <typename T> struct helper {
static void print(T value) { std::cout << value; }
};
vector_helper.cpp
#include <vector>
#include <iostream>
template <typename T> struct helper<std::vector<T>> {
static void print(std::vector<T> const &value) { }
};
print.hpp
#include "helper.hpp"
template <typename T>
void print (T const &value) {
// Just delegate.
helper<T>::print (value);
}
main.cpp
#include "print.hpp"
int main () {
print (5);
std::vector<int> v;
print (v);
}
このようにコンパイルされます:
g++ main.cpp vector_helper.cpp
問題は、MinGWがリンク時エラーを生成していることです:未定義の参照helper<vector<...>>::print(vector<...>)
行を追加すると:
#include "vector_helper.cpp"
以前int main() {...}
は、正常にコンパイルされ、動作します。g ++コマンドでリンクされたファイルにクラスの特殊化を追加したいので、どうすれば解決できますか。