나는이 질문에 다음, 아이겐 유형에 대한 템플릿 특수화 몇 가지 함수를 작성하는 것을 시도하고있다 : Eigen: type deduction in template specialization of base-class템플릿 전문 기능
나는이 쓴 :
#include <type_traits>
#include <Eigen/Core>
namespace isEigenPlainObjectBaseDetail {
template <typename T>
std::true_type test(const Eigen::PlainObjectBase<T>);
std::false_type test(...);
}
template <typename T>
struct isEigenPlainObjectBase :
public decltype(isEigenPlainObjectBaseDetail::test(std::declval<T>())) {};
template <typename T, typename Enable = void>
void foo(T& obj) {
std::cout << "Generic Called!\n";
}
template <typename T, typename std::enable_if<isEigenPlainObjectBase<T>::value>::type>
void foo(T& obj) {
std::cout << "Eigen Specialization Called!";
}
int main() {
Eigen::MatrixXd m;
Eigen::VectorXd v;
int i = 0;
foo(i);
foo(m);
foo(v);
return 0;
}
을하지만 매번 일반적인 함수를 호출 :
Generic Called!
Generic Called!
Generic Called!
내가 뭘 잘못하고 있니? Eigen 행렬 및 벡터가있는 함수의 템플릿 전문화는 어떻게 작성합니까?
두 번째 함수는 오버로드이며, 두 번째 템플릿 매개 변수가 비 유형 변수이기 때문에 유효하다고 생각합니다. 그러나'std :: enable_if <> :: type'은'void'이기 때문에 항상 SFINAE가 출력되지 않습니다. 이것은 허용되지 않습니다. – Quentin
@Quentin - 'typename = typename std :: enable_if'가 아니라'typename std :: enable_if' 만 주목했습니다. 대답이 명시 적으로 수정되었습니다. 감사! – max66
알았어, 좋은 정보! : D 나는 gcc로 컴파일했고, msvc로 시도하지 않았다. 그러나 물론 그것은 작동하지 않고 있었다! 템플릿을 사용하여 모든 고유 일반 객체를 구분할 수있게 해주는 Eigen 라이브러리에서 설계된 것을 기대하고있었습니다! –