2014-03-26 67 views
0

我正在研究一些與數學相關的對象,我希望能夠區分以數學方式表現的操作符。下面是最簡單的解決方案,我希望能夠以某種方式完成。在外部函數中訪問Typedef

struct derp { }; 

derp operator+(derp const & a, derp const & b) { 
    typedef std::true_type commutative; 
    typedef std::true_type associative; 

    return derp(); 
} 

void test() { 
    typedef int testing; 
} 


int main(int argc, const char * argv[]) { 
    // is it possible to access test::testing or the typedef's in the operator+ from here? 

    return 0; 
} 

這是或類似的可能嗎?

+0

你是什麼意思的訪問。一個typedef實際上並不實例化一個變量。它幾乎定義了一種數據類型,所以沒有任何東西可以訪問。如果你的意思是你想使用那些不同的數據類型。 – dboals

+0

我需要訪問模板元編程目的的類型 – pat

+0

不可以,因爲'test'不是一個類。試着用'test'作爲成員來定義一個類,''測試'也作爲一個成員,但在'test'之外。另外'test :: testing'這個表示方法不正確('test'不是一個類)。 – rullof

回答

0

typedef s在一個函數內是函數的局部範圍。

一種提供類似功能的方法是定義一個外部類型(也可以是一個嵌套類型),它定義了你想要的。

一種可能的方式:

template<typename T> 
struct arithmetic_traits 
{ 
    using addition_associate = std::false_type; 
    using addition_commutative = std::false_type; 
}; 

template<> 
struct arithmetic_traits<derp> 
{ 
    using addition_associate = std::true_type; 
    using addition_commutative = std::true_type; 
};