English 中文(简体)
使用静态成员作为命名空间,
原标题:Using static members to act as a namespace with templates : bad idea?
  • 时间:2012-05-26 13:19:18
  •  标签:
  • c++
  • c++11

我有个关于设计的问题

我有一系列通用功能 不属于同一个物体 而是以相同的复杂类型行事

我想把他们归为一个模版类, 成为静态成员功能, 原因有两个:

a) I can put typedefs into my "namespace class" so that code is more readable b) The user can typedef the static class once and for all and then forget about specializing the functions.

而不是呼唤:

my conference< char,int,std::set< 双 & gt; & gt; (...);

他可以做到:

typedef TheClasss< char, int, std:: 设置 < 双 & gt; & gt; MyTheClass;

并写:

MyTheClass::: 我的功能(...);

你会考虑这个糟糕的设计以及为什么吗?

提前感谢!

最佳回答

我认为无论你在哪里都能应用DRY原则, 通常都是个好主意, 所以如果这样可以避免反复通过相同的模板参数, 无法找到更干净的替代方法, 那就去做。

问题回答

类中的模板函数只有在使用时才会被即时化。因此,即使您重新将所有函数包在模板类中,它也不会是无效的,因为只有使用特定类型参数集的模板才会用该类型参数集即时化。

从可读性的角度来说,我个人鄙视打字机,除非在罕见的情况下,因为你最终不得不追逐他们去弄清楚你的类型,而当你有很多打字机的班级使用不同的类型参数时,它就会变得令人沮丧,而且很难读懂。

也许在他们自己的真名空间中具有类似目的的换行函数, 可以在您想要使用它们时用一个使用条款来引入, 并且为可选的 typefs 另设一个命名空间, 用户可以为这些函数单独引入 :)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签