【问题标题】:problems about name for namespace and template parameters关于命名空间和模板参数的名称问题
【发布时间】:2020-01-31 16:16:21
【问题描述】:

最近看了一些关于websocketpp的代码,下面的代码是我比较疑惑的:

#include <somefile.hpp>
template <typename config>
class endpoint : public config::socket_type {          //--------A--------//
    typedef typename config::concurrency_type concurrency_type;//-----B------//
    ....
}

在 somefile.hpp 中,有一个命名空间,也称为 config。所以我不知道 A 和 B 中的名称“config”是表示该命名空间还是模板参数。 我猜是模板参数。

感谢所有问我问题的人。

【问题讨论】:

    标签: c++ c++11 templates namespaces


    【解决方案1】:

    模板参数的名称是在主模板声明的范围内引入的。它隐藏了命名空间的名称。

    来自 C++ 17 标准(6.3.2 声明点)

    11 模板参数的声明点是立即 在其完整的模板参数之后

    使用 requires 子句,模板声明可能看起来更清晰。

    这是一个演示程序

    namespace config
    {
    }
    
    template <typename config> requires requires { typename config::socket_type; }
    class endpoint : public config::socket_type
    {
    };
    
    struct A
    {
        struct socket_type {};
    };
    
    int main()
    {
        endpoint<A> e;
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2011-03-27
      相关资源
      最近更新 更多