混合 Class 和方法模板的 C++ 模板语法
C++ Template Syntax for Mixed Class and Method Templates
我无法让内联方法实现引用 通用模板实例方法 [检查命名法] 因为我不知道如何引用它(或者不能'吨)。我可以让它内联工作,但想知道 if/how 延迟定义。
我发现了具有相同设置(class/struct 模板参数和方法实例参数)的类似问题,但它们总是在解决不同的问题(我认为)。
#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>();
// }
在 GCC 上取消注释 fmap2
。
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
内联定义有效,但我想稍后在
如果可能的话编译单元。
提前致谢。
你需要先为Vec
声明typename T
,然后为fmap2
声明typename R
,像这样
template <typename T>
template <typename R>
Vec<R> Vec<T>::fmap2(std::function<R(T)> apply) const {
return Vec<R>();
}
我无法让内联方法实现引用 通用模板实例方法 [检查命名法] 因为我不知道如何引用它(或者不能'吨)。我可以让它内联工作,但想知道 if/how 延迟定义。
我发现了具有相同设置(class/struct 模板参数和方法实例参数)的类似问题,但它们总是在解决不同的问题(我认为)。
#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>();
// }
在 GCC 上取消注释 fmap2
。
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
内联定义有效,但我想稍后在 如果可能的话编译单元。
提前致谢。
你需要先为Vec
声明typename T
,然后为fmap2
声明typename R
,像这样
template <typename T>
template <typename R>
Vec<R> Vec<T>::fmap2(std::function<R(T)> apply) const {
return Vec<R>();
}