[英]C++ template function resolution in splitted namespaces
為什么編譯器在以下命名空間中聲明模板函數時無法解析模板函數?
工作:
namespace space {
template<> void f(T);
template<> void f(U);
}
void g() {f(U{});}
工作:
template<> void f(T);
void g() {f(U{});}
template<> void f(U);
不行:
namespace space {
template<> void f(T);
}
void g() {f(U{});}
namespace space {
template<> void f(U);
}
工作:
https://wandbox.org/permlink/U8tS3L1nkmGD9m7X
https://wandbox.org/permlink/dxmHD7SMz6uPdEHc
不行:
https://wandbox.org/permlink/FrEBMETzHD1KuHUx
以及如何使不工作的工作?
它和任何其他函數一樣,在使用函數時,至少要聲明它。
並且因為您在定義template<> void f(U);
之前移動了您的g()
(您調用f(U{})
template<> void f(U);
並且您在g()
之前沒有聲明它,這將導致丟失聲明錯誤。 所以你至少需要在g()
之前聲明模板函數:
namespace space {
template<> void f(T);
template<> void f(U); // declaration
}
void g() {
f(U{});
}
namespace space {
template<> void f(U) { // definition
// code
}
}
如果是您的真實代碼:
// your first detail namespace
namespace detail {
// […] the other template functions
// the declaration of the two functions that are defined in the second detail namespace
template <typename T, typename U>
auto smart_division_impl(T a, U b, rank<1>) -> decltype(a * (U(1)/b));
template <typename T, typename U>
int smart_division_impl(T, U, rank<0>);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.