assert语句中模板结构实例化语法错误



对于下面在头文件中定义的模板结构体:

#include <unordered_set>
namespace Utilities::ContainerHelpers
{
template <template <class> class TContainer, class TVal>
struct is_unique
{
bool operator()(TContainer<TVal> const& container) const
{
std::unordered_set<TVal, typename TVal::HashFunc> 
uniqueSubset(container.begin(), container.end());
return container.size() == uniqueSubset.size();
}
};
}

在assert上下文中使用时会出现语法错误:

assert(Utilities::ContainerHelpers::is_unique< std::vector, TelemDetail >{}(telem_detail_obj);

,但当定义为assert之外的临时变量时则不是。TelemDetail类型不重要,只是包含一个嵌套的HashFunc结构类型。

警告C4002:类函数宏调用'assert'参数太多

错误C2059:语法错误:')'

感觉我错过了一些明显的东西?使用MSVC 2019 -std=c++17编译

assert和每个宏一样,不理解c++,它把每个逗号,当作参数分隔符:

assert(is_unique< std::vector, TelemDetail >{}(session_details));
//    (   first arg          ,  second arg                     )
//                                   

但是预处理宏"理解"()-所以只需添加额外的一对():

assert((is_unique< std::vector, TelemDetail >{}(session_details)));

最新更新