繁体   English   中英

断言语句中模板结构实例化的语法错误

[英]Syntax error for template struct instantiation inside assert statement

对于 header 中定义的以下模板结构:

#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(Utilities::ContainerHelpers::is_unique< std::vector, TelemDetail >{}(telem_detail_obj);

但不是在断言之外定义为临时的。 TelemDetail类型并不重要,只是包含一个嵌套的HashFunc结构类型。

警告 C4002:对于类似函数的宏调用“断言”,arguments 过多

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

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

assert ,作为每个宏,不理解 C++ ,它将每个逗号视为 arguments 分隔符:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM