2013-02-21 124 views
0

我可以使用C++模板類來區分對象類型嗎?或者我應該使用什麼?模板類來區分對象類型?

例如,例如,我有一個類Synonym,它可以是Statement, Procedure, etc類型。我有接受這些同義詞並根據其類型對它們進行評估的函數。所以我想這將是很好,如果我可以這樣做:

enum Types { Statement, Procedure, Variable, ... }; 

template <typename Types> 
class Synonym { ... } 

void evaluate(Synonym<Statement> s, Synonym<Variable> v) { do something } 
      ^so that I can do this ... instead of checking the type in function like: 

void evaluate(Synonym s, Synonym v) { 
    assert(s.type == Statement); 
    assert(v.type == Variable); 

    // also would like to eliminate things like: (if possible) 
    switch(s.type) { 
     case XXX: doSomething ... 
     case YYY: doAnotherThing ... 
    } 
} 
+0

我不明白,你想達到什麼目的? – StoryTeller 2013-02-21 14:12:45

+0

你總是可以重載函數參數,所以是第一次評估應該沒問題。 – StoryTeller 2013-02-21 14:14:10

回答

0

您可以創建一個函數模板,然後專注於該模板

template<typename Type> 
void evaluate (Type t) {} 

template<> 
void evaluate<Statement>(Statement s) 
{} 

這樣,當你傳遞一個Statement它會選擇這種超載,並且您可以根據類型執行不同的行爲。

0

我認爲使用變體和訪問者模式是適合的。看看Boost.Variant在這裏:http://www.boost.org/doc/libs/1_51_0/doc/html/variant.html,最後一個例子(也在下面,但展開)顯示了一個訪客實現。還有其他變體和訪問者實現。 std :: any和loki也是選項。我個人喜歡洛基,但這可能只是因爲我是亞歷山德里斯庫的忠實粉絲。

#include "boost/variant.hpp" 
#include <iostream> 

class ToLengthVisitor : public boost::static_visitor<int> 
{ 
public: 
    int operator()(int i) const 
    { 
     return i; 
    } 

    int operator()(const std::string & str) const 
    { 
     return str.length(); 
    } 

    int operator()(const char * str) const 
    { 
     const char * temp = str; 
     while(*temp != '\0') temp++; 
     return temp-str; 
    } 
}; 

int main() 
{ 
    typedef boost::variant< int, std::string, const char * > MyVariant; 
    MyVariant u(std::string("hello world")); 
    std::cout << u; // output: hello world 

    MyVariant cu(boost::get<std::string>(u).c_str()); 

    int result = boost::apply_visitor(ToLengthVisitor(), u); 
    std::cout << result; // output: 11 (i.e., length of "hello world") 
    result = boost::apply_visitor(ToLengthVisitor(), cu); 
    std::cout << result; // output: 11 (i.e., length of "hello world") 
}