Phi*_*erg 6 c++ templates namespaces
我正在尝试编写一个库,其中有一些模板化函数,其中一些是辅助函数,因此我不希望我的用户可以访问它们.一些基本代码可能是
//mylib.h
namespace myfuncs
{
template<class T>
void helper (T input, int extrainformation)
{
//do some usefull things
}
template<class T>
void dostuff(T input)
{
int someinfo=4;
helper(input, someinfo);
}
}
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式隐藏辅助函数,以便库的用户不能直接调用它吗?我原以为一个未命名的命名空间可能会起作用,但因为我使用的是模板,所以我无法在头文件和实现文件之间拆分函数声明和主体.将未命名的命名空间放在头文件中是没有用的,也不好用.我唯一能想到的就是创建一个mylib类并将这些函数封装为私有/公共静态函数.
任何更好的解决方案将不胜感激.
菲尔
一种方法是使用"细节"或"内部"命名空间.这是多少个图书馆.
namespace myfuncs
{
namespace detail
{
template<class T>
void helper (T input, int extrainformation)
{
//do some usefull things
}
}
template<class T>
void dostuff(T input)
{
int someinfo=4;
detail::helper(input, someinfo);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1600 次 |
| 最近记录: |