如何隐藏实施帮助模板? [英] How to hide an implementation helper template?

查看:58
本文介绍了如何隐藏实施帮助模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在头文件中声明了两个模板函数:

Suppose that I have two template functions declared in a header file:

template <typename T> void func1(const T& value);
template <typename T> void func2(const T& value);

并假设这些功能的实现(也在头文件中,而不在源文件中,因为它们是模板)使用了一些实现帮助程序功能,该功能也是模板:

And suppose that the implementation of these functions (also in a header file and not in a source file, because they are templates) uses some implementation helper function, which is also a template:

template <typename T> void helper(const T& value) {
    // ...
}

template <typename T> void func1(const T& value) {
    // ...
    helper(value);
}

template <typename T> void func2(const T& value) {
    // ...
    helper(value);
}

在我包含头文件的任何源文件中,辅助函数将是可见。我不需要,因为辅助功能只是实现细节。有没有办法隐藏辅助函数?

In any source file that I include the header file, the helper function will be visible. I don't want that, because the helper function is just an implementation detail. Is there a way to hide the helper function?

推荐答案

一种常见方法(例如,在许多Boost库中使用的方法)是将帮助程序放在名为 details 的命名空间中,可能在单独的标头(包含在 public标头中)中。

A common approach (as used in many Boost libraries, for example) is to put the helper in a namespace called details, possibly in a separate header (included from the "public" header).

无法阻止它可见和可调用,但这很清楚地表明它是实现的一部分,而不是接口。

There's no way to prevent it from being visible, and callable, but this quite clearly indicates that it is part of the implementation, not the interface.

这篇关于如何隐藏实施帮助模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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