如何转发声明已在其他地方默认声明的模板类型 [英] How do I forward declare a template type that has been forward declared elsewhere with a defaulting

查看:118
本文介绍了如何转发声明已在其他地方默认声明的模板类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,此问题的绝佳答案指出,您可以在正向声明中默认模板类型,但是: >

So the excellent answer to this question states that you can default template types in a forward declaration, however:

每个默认模板参数只能指定一次

You can specify each default template argument only once

在链接的问题中对此进行了更详细的说明,但是Boost的属性树"内容适用于ptree类型:

This is more fully specified in the linked question, but Boost's Property Tree stuff works with the ptree type:

typedef basic_ptree<std::string, std::string> ptree;

但是basic_ptree类的定义如下:

template<class Key, class Data, class KeyCompare>
class basic_ptree

仅使用2个模板参数定义ptree typedef的唯一原因是,在typedef之前存在前向声明:

The only reason the ptree typedef is defined with only 2 template parameters is that before it's typedef there is the forward declaration:

template < class Key, class Data, class KeyCompare = std::less<Key> >
class basic_ptree;


这使我想到了我的实际问题.我需要绕过ptree.但是我不想在标题中包含Boost.这意味着我必须转发声明. 但是我无法匹配默认的前向声明,因为默认只能声明一次.


This brings me to my actual question. I need to pass around a ptree. But I don't want to include Boost in my headers. This means that I'll have to forward declare. But I can't match the defaulted forward declaration because the default can only be declared once.

如何编写依赖于另一个标头正向声明中的默认值的正向声明和正向typedef?

我目前正在这样做,但这意味着我转发typedef ing,我正在制作 new typedef:

I'm currently doing this, but that means I'm not forward typedef'ing, I'm making a new typedef:

template <class Key, class Data, class KeyCompare> class basic_ptree;
typedef basic_ptree<std::string, std::string, std::less<std::string>> ptree; 

为了保持一致,我想依赖原始ptree typedef所依赖的相同的默认默认声明basic_ptree.

For the sake of consistency I want to depend on the same defaulted forward declaration of basic_ptree that the original ptree typedef depended upon.

推荐答案

简短的答案是你做不到.您要么必须保证以某种方式永远不会包含另一个声明,所以您可以指定自己的默认声明,或者您需要依赖另一个声明.

The short answer is that you can't. You either have to guarantee somehow that the other declaration will never be included so you can specify your own default, or you need to depend on that other declaration.

但是我感觉这是一个 XY问题,用于以下语句:

But I get the feeling this is an XY problem, for the following statement:

但是我不想在标题中包含Boost.

But I don't want to include Boost in my headers.

为什么不呢?您的代码显然取决于Boost的ptree,因此任何使用您代码的人都必须安装Boost.如果您希望声明在未安装Boost的情况下也能编译,则有以下几种选择:

Why not? Your code is obviously dependent on Boost's ptree, so anyone working with your code would have to have Boost installed anyway. If you want your declarations to also compile when Boost isn't installed, there are several options:

  • 确保使用预处理器指令(#if/#endif块)排除使用ptree的位
  • 使用C ++ 17的__has_include()测试<boost/property_tree/ptree.hpp>的存在,然后将其包含(如果存在),或者声明您自己的ptree(不存在).
  • Make sure you exclude the bits that use ptree using preprocessor directives (#if/#endif blocks)
  • Use C++17's __has_include() to test for the existence of <boost/property_tree/ptree.hpp>, and then either include it if it exists, or declare your own ptree if it doesn't.

这篇关于如何转发声明已在其他地方默认声明的模板类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆