2013-02-21 47 views
8

C++中是否存在嚴格typedef的習慣用法,可能使用模板?C++中嚴格typedef的成語

喜歡的東西:

template <class base_type, int N> struct new_type{ 
    base_type p; 
    explicit new_type(base_type i = base_type()) : p(i) {} 
}; 

typedef new_type<int, __LINE__> x_coordinate; 
typedef new_type<int, __LINE__> y_coordinate; 

所以我可以做這樣的一個編譯時錯誤:

x_coordinate x(5); 
y_coordinate y(6); 

x = y; // whoops 

中的__LINE__那裏看起來可能很麻煩,但我寧願不必須手動創建一組常量來保持每種類型的唯一性。

+1

座標是不是這是一個非常好的應用。兩個座標的乘積不是座標等。您可能想要查看Boost.Units。 – 2013-02-21 15:40:06

+2

我刪除了我的答案,其中提示[BOOST_STRONG_TYPEDEF](http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/strong_typedef.html),因爲它顯然適用於重載分辨率,但不會生成編譯錯誤交叉分配。 – Angew 2013-02-21 15:42:24

+0

我質疑你想做什麼的價值。例如,當你想要執行旋轉時會發生什麼? 'x = sin(theta)* y + cos(theta)* x'應該是完全有效的。 – 2013-02-21 16:46:38

回答

5

我在我的項目中使用了類似的東西。只有我使用類型標記而不是int。在我的特殊應用程序中運行良好

template <class base_type, class tag> class new_type{  
    public: 
    explicit new_type(base_type i = base_type()) : p(i) {} 

    // 
    // All sorts of constructors and overloaded operators 
    // to make it behave like built-in type 
    // 

    private: 
    base_type p; 
}; 

typedef new_type<int, class TAG_x_coordinate> x_coordinate; 
typedef new_type<int, class TAG_y_coordinate> y_coordinate; 

注意TAG_ *類並不需要在任何地方定義,他們只是標籤

x_coordinate x (1); 
y_coordinate y (2); 

x = y; // error 
+0

我認爲用簡單的「#define new_type(base,tag)typedef new_type <## base,class TAG _ ## tag> tag; – slacy 2013-03-13 23:55:04

+1

@slacy dunno,我覺得宏很醜,並避免它們 – 2013-03-14 11:25:03

2

不可以。有建議讓它進入下一個標準(C++ 14或C++ 17),但不適用於C++ 11。

+1

-1:他沒有問是否使用語言,並且有方法可以在「用戶空間」。因此,這個答案是不正確的。 – 2013-02-21 15:26:23

+0

在用戶空間中沒有辦法與提議的語言特徵相同。 – Puppy 2013-02-21 18:34:06

+0

這與問題中陳述的內容相同嗎?既然Angew的答案已經失效了,我很樂意接受這個可能性,但是需要你的輸入。 – 2013-02-21 20:00:54

0

用C++ 11:

#include <stdio.h> 

struct dummy {}; 

struct NotMineType 
{ 
    NotMineType(dummy) {} 
}; 

template <int N> 
struct type_scope 
{ 
    struct MyOwnType 
    { 
    }; 

    struct ConvertedToMineType : NotMineType 
    { 
     template <typename ...Args> 
     ConvertedToMineType(Args... args) : NotMineType(args...) {}; 
    }; 

    enum myint : int {}; 
}; 

typedef type_scope<0>::MyOwnType x1; 
typedef type_scope<1>::MyOwnType x2; 

typedef type_scope<0>::ConvertedToMineType y1; 
typedef type_scope<1>::ConvertedToMineType y2; 

typedef type_scope<0>::myint i1; 
typedef type_scope<1>::myint i2; 

void foo(x1) { printf("x1\n"); } 
void foo(x2) { printf("x2\n"); } 
void foo(y1) { printf("y1\n"); } 
void foo(y2) { printf("y2\n"); } 
void foo(i1) { printf("i1\n"); } 
void foo(i2) { printf("i2\n"); } 

int main() 
{ 
    foo(x1()); 
    foo(x2()); 
    foo(y1(dummy())); 
    foo(y2(dummy())); 
    foo(i1()); 
    foo(i2()); 
} 

輸出:

x1 
x2 
y1 
y2 
i1 
i2 

編譯器:

的Visual Studio 2015年,GCC 4.8.x