Created
November 5, 2013 07:40
-
-
Save wwssllabcd/7315289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
抱歉我在講仔細一點,以下是您的code | |
template | |
< | |
class Subject, | |
template< class > class ReadingPolicy, | |
template< class > class WritingPolicy, | |
template< class > class ErrorHandlingPolicy | |
> | |
class ResourceIOManager | |
: | |
public ReadingPolicy< Subject >, | |
public WritingPolicy< Subject >, | |
public ErrorHandlingPolicy< Subject > | |
{ | |
...略.... | |
}; | |
Policy 藉由 Subject 被推導出來,來源是 Compiler 自動推導,不能更換,也就是說,Policy 依賴於 Subject | |
改成下面的 code ,使用預設值代替 | |
template | |
< | |
class Subject, | |
typename RP = ReadingPolicy< Subject >, | |
typename WP = WritingPolicy< Subject >, | |
typename EP = ErrorHandlingPolicy< Subject >, | |
> | |
class ResourceIOManager | |
: | |
public RP, | |
public WP, | |
public EP | |
{ | |
...略.... | |
}; | |
這樣不失自動推導的優點,而且有機會可以使用 | |
ResourceIOManager<T, ReadingPolicy<U>, WritingPolicy< R >, ErrorHandlingPolicy< S >, > | |
這種組合 | |
用這個方法 policy 不一定要依賴 T | |
這方法感覺似乎比較好,但神人 Adnrei Alexanderscu 主要還是使用 template template parameter, 故私認為這樣做法似乎有某種缺陷?煩請版主賜教 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,原作者會採用第一個做法,個人猜想,應該是為了維持 subject 物件與各項 policy 之間的關係吧。因為在實作中必定會以 policy class 來操作 subject 這個主體物件。在 read, write, error handling 這組功能中,操作對象應該是單一類別,所以一般情況下,應該不會需要傳入不同的 U, R, S 物件才是。