Creating a new primitive type
Asked Answered
I

3

5

Is there a way to create a new type that is like one of the basic types (eg char), and can be implcitly converted between, but will resolve diffrently in templates, such that for example, the following code works?

typedef char utf8;
template<typename T>void f(T c);
template<> void f<char>(char c)
{
    std::cout << "ascii " << c << std::endl;
}
template<> void f<utf8>(utf8 c)//error C2766: explicit specialization; 'void f<char>(char)' has already been defined
{
    std::cout << "utf8 " << c << std::endl;
}
int main()
{
    char c1 = 'x';
    utf8 c2 = 'g';
    f(c1);
    f(c2);
}

I'm thinking that it may be possible with a class containing a single data member, if so what is the cleanest way to do it, and will compilers be able to optimise it as if it was a primitive.

EDIT: I tried BOOST_STRONG_TYPEDEF, and that seems to work for basic things, but how can I then go onto create a std::basic_string from the new type?

BOOST_STRONG_TYPEDEF(char,utf8);
//first try
BOOST_STRONG_TYPEDEF(std::string,utf8_string);
//second try
typedef std::basic_string<utf8, std::char_traits<utf8>,std::allocator<utf8> > uft8_string;

The first one doesn't really work because the resulting type still expects char for all its methods, and the second one doesn't seem to like the presence of constructors and assignment operators :(

I haven't tried the other way to create the new char type yet, will it be able to get around this if I cant with BOOST_STRONG_TYPEDEF?

Indent answered 15/10, 2009 at 13:36 Comment(2)
The flaw in your plan is that despite its name UTF-8 is not 8-bits wide. It is variable width from 8 to 32bits. A single char can therefore only represent a tiny subset of UTF-8 (in fact the 128 character ASCII subset).Kadner
That is why I want types that can use different template specialisations and overloads, so that it will use versions of the method that take account of the fact that UTF-8 (and UTF-16) may use multiple elements for a single char.Indent
P
4

I heard a rumour that C++0x will bring strong typedefs which will allow for the utf8 class in your case to be distinguishable from the char, but that doesn't exist currently. Maybe Boost's strong typedef would help, but I don't know.

Picture answered 15/10, 2009 at 13:42 Comment(0)
V
11

C++ author Matthew Wilson calls what you're describing "true typedefs." They're implemented as a class containing a single data member (just like you said). He describes them and provides a template implementation in chapter 18 of Imperfect C++ (good book, by the way) and in this article in Dr. Dobb's Journal.

Regarding optimization, any good compiler should be able to optimize this as a primitive (since large parts of the STL, for example, rely on good quality optimizations like this), but I don't have any specifics.

Vergeboard answered 15/10, 2009 at 13:42 Comment(1)
True; std::vector<T> iterators are thin wrappers for T* and a good optimizer needs to deal with that.Predecease
P
4

I heard a rumour that C++0x will bring strong typedefs which will allow for the utf8 class in your case to be distinguishable from the char, but that doesn't exist currently. Maybe Boost's strong typedef would help, but I don't know.

Picture answered 15/10, 2009 at 13:42 Comment(0)
H
0

The problem here is that typedef doesn't really define new types (like say Ada, where you can define mutually incompatable integer types), just aliases for existing ones.

I believe one of the proposals for the next version of C++ will include proper types.

Hypethral answered 15/10, 2009 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.