我有麻煩的直線排列方式實作參考一個通用模板實體方法[查看命名],因為我不知道如何參考它(或不能)。我可以把它行內作業,但想知道是否/如何做延遲的定義。
我發現有相同的設定(類/結構模板引數和方法實體引數)類似的問題,但他們總是解決不同的問題(我認為)。
#include <functional>
template <typename T>
struct Vec {
T elem;
Vec(T t) : elem(t) { }
// this works, but I want to defer the definition to later
template <typename R>
Vec<R> fmap(std::function<R(T)> apply) const {
return Vec<R>(apply(elem));
}
// but I want to defer the definition to later in the file
// template <typename R>
// Vec<R> fmap2(std::function<R(T)> apply) const;
};
// This FAILS: how do I refer to this?
// template <typename T,typename R>
// inline Vec<R> Vec<T>::fmap2(std::function<R(T)> apply) const {
// return Vec<R>();
// }
在取消fmap2對GCC給出。
no declaration matches ‘Vec<R> Vec<T>::fmap2(std::function<R(T)>) const’
的Visual Studio 2019提供了
unable to match function definition to an existing declaration
行內定義的作品,但我想如果可能的話,以后來定義模板成員在編譯單元。
提前致謝。
uj5u.com熱心網友回復:
你需要先申報typename T,Vec然后typename R是fmap2,就像這樣
template <typename T>
template <typename R>
Vec<R> Vec<T>::fmap2(std::function<R(T)> apply) const {
return Vec<R>();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/440104.html
上一篇:C:Windows沒有找到HARDWARE\DEVICEMAP\SERIALCOMM,即使可以使用regedit找到它
