【发布时间】:2019-10-26 16:58:36
【问题描述】:
我有一个函数对象,它是另一个函数的包装器:
template <typename FuncT>
class Wrapper
{
private:
FuncT funcToWrap;
public:
Wrapper(FuncT ftw) : funcToWrap(ftw){};
template<typename ...ARG>
typename std::result_of<FuncT(ARG&&...)>::type operator()(ARG&&... args){
return funcToWrap(std::forward<ARG>(args)...);
}
};
int main(){
std::function<void()> testfunc = [](){ std::cout << "Test" << std::endl; };
Wrapper<decltype(testfunc)> test{testfunc};
test();
}
如果std::result_of<FuncT(ARG&&...)>::type 不是void,我想做的是将operator() 标记为[[nodiscard]]。
我注意到的是,当我将 [[nodiscard]] 用于返回类型的模板评估到 void 时,它只会被我的编译器忽略。
这是我可以依赖的行为吗,是否以任何方式标准化?
【问题讨论】:
-
这显然是not guaranteed by the standard,但我没有找到来自 gcc 或 clang 的关于 void nodiscard 的文档 :(
-
std::result_of是 deprecated in C++17 and removed in C++20。请改用std::invoke_result。 -
标准似乎未指定。但老实说,我只是把属性放在那里而不担心。属性是对编译器的非绑定提示。只要它们在语法上是正确的,它们就不会将原本格式良好的程序变成格式错误的程序。
-
我想知道是否应该将其报告为文档错误...
-
解决缺乏保证的问题:wandbox.org/permlink/xAQvlhtB6CXU8YnV
标签: c++ attributes c++17 nodiscard