【发布时间】:2020-12-07 18:41:48
【问题描述】:
我有这门课:
template <typename T>
class Value {
private:
bool fixed;
union {
T value;
std::function<T()> get;
};
public:
Value(const T& value);
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F>>>
Value(F&& get);
Value(const T* pointer);
~Value();
operator T();
};
我最初将其全部写在头文件中,但现在将代码转移到.cpp 文件中。问题是我不知道在定义Value(F&& get) 时如何包含typename = std::enable_if_t<std::is_invocable_v<F>> 部分。
首先,我试过了:
template <typename T>
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F>>>
Value<T>::Value(F&& get) : fixed(false), get(std::forward<F>(get)) {}
产生错误的原因:
error: a default template argument cannot be specified on the declaration of a member of a class template outside of its class
然后,看了this answer之后,我尝试了:
template <typename F>
std::enable_if_t<std::is_invocable_v<F>>
Value<T>::Value(F&& get) : fixed(false), get(std::forward<F>(get)) {}
导致:
error: return type may not be specified on a constructor
我应该怎么做?
【问题讨论】:
-
这样做stackoverflow.com/questions/495021/…。间接回答你的问题?
-
只需在定义中省略默认参数即可。但是您确定要将模板移动到
.cpp文件吗?一般来说,这不是一个好主意。 -
@super 我相信他们会去
#include标题内的.cpp文件。这对单个template类是否有害? -
@Cem 只是令人困惑。如果您要在头文件中包含定义,将其拆分为
.cpp并包含它只会让阅读代码的人和一些静态分析工具等都想知道发生了什么。我见过有人这样做,但是他们通常将文件命名为.tpp或其他名称以表明它不是编译单元。
标签: c++ class templates constructor typename