Create Generic Class with String?

I

Immortal Nephi

I create generic class. The type T is to be std::string or
std::wstring. I declare t variable to static const because I do not
want to modify string. C++ Compiler complies without errors.
If you add std::cout to the main function body, then C++ Compiler
will fail to compile and the error message says

c:\my projects\colors\colors\main.cpp(1438) : error C2373: 't' :
redefinition; different type modifiers
c:\my projects\colors\colors\main.cpp(1434) : see declaration
of 't'
c:\my projects\colors\colors\main.cpp(1438) : while compiling
class template static data member 'const std::string Foo<T>::t'
with
[
T=std::string
]
c:\my projects\colors\colors\main.cpp(1444) : see reference to
class template instantiation 'Foo<T>' being compiled
with
[
T=std::string
]

template< typename T >
class Foo
{
public:
Foo<T>() {}
~Foo<T>() {}

static const T t;
};

template< typename T >
T Foo<T>::t = "Hello World!\n";

int main()
{
Foo<std::string> f;
std::cout << f.t;

return 0;
}

What happen if I declare std::wstring instead of std::string? C++
Compiler will fail to compile because ‘L’ is not added before string
”Hello World!\n”.
I do not want to create two separate classes. One is for string and
another one is for wstring. I add preprocessor condition.

#if !defined( UNICODE )
template< typename T >
std::string Foo<T>::Text = "Hello World!\n";
#else
template< typename T >
std::wstring Foo<T>::wText = L"Hello World!\n";
#endif

Do you have the solution?
 
Ö

Öö Tiib

        I create generic class.  The type T is to be std::string or
std::wstring.  I declare t variable to static const because I do not
want to modify string.  C++ Compiler complies without errors.
        If you add std::cout to the main function body, then C++ Compiler
will fail to compile and the error message says

c:\my projects\colors\colors\main.cpp(1438) : error C2373: 't' :
redefinition; different type modifiers
        c:\my projects\colors\colors\main.cpp(1434) : see declaration
of 't'
        c:\my projects\colors\colors\main.cpp(1438) : while compiling
class template static data member 'const std::string Foo<T>::t'
        with
        [
            T=std::string
        ]
        c:\my projects\colors\colors\main.cpp(1444) : see reference to
class template instantiation 'Foo<T>' being compiled
        with
        [
            T=std::string
        ]

template< typename T >
class Foo
{
public:
        Foo<T>() {}
        ~Foo<T>() {}

        static const T t;

};

template< typename T >
T Foo<T>::t = "Hello World!\n";

int main()
{
        Foo<std::string> f;
        std::cout << f.t;

        return 0;

}

        What happen if I declare std::wstring instead of std::string?  C++
Compiler will fail to compile because ‘L’ is not added before string
”Hello World!\n”.
        I do not want to create two separate classes.  One is for string and
another one is for wstring.  I add preprocessor condition.

#if !defined( UNICODE )
        template< typename T >
        std::string Foo<T>::Text = "Hello World!\n";
#else
        template< typename T >
        std::wstring Foo<T>::wText = L"Hello World!\n";
#endif

        Do you have the solution?

Yes, you need std::wcout to << std::wstring.

Generally, it is bad idea to slice your code with preprocessor like
that. Better stick with explicit wchar_t or char. Microsoft has done
that thing with their TCHAR and despite tremendous manpower that
company has their solution outright leaks memory, looks like crap and
is very inefficient when compiled without unicode character set.
 
I

Immortal Nephi

        I create generic class.  The type T is to be std::string or
std::wstring.  I declare t variable to static const because I do not
want to modify string.  C++ Compiler complies without errors.
        If you add std::cout to the main function body, then C++ Compiler
will fail to compile and the error message says
c:\my projects\colors\colors\main.cpp(1438) : error C2373: 't' :
redefinition; different type modifiers
        c:\my projects\colors\colors\main.cpp(1434) : see declaration
of 't'
        c:\my projects\colors\colors\main.cpp(1438) : while compiling
class template static data member 'const std::string Foo<T>::t'
        with
        [
            T=std::string
        ]
        c:\my projects\colors\colors\main.cpp(1444) : see reference to
class template instantiation 'Foo<T>' being compiled
        with
        [
            T=std::string
        ]
template< typename T >
class Foo
{
public:
        Foo<T>() {}
        ~Foo<T>() {}
        static const T t;

template< typename T >
T Foo<T>::t = "Hello World!\n";
int main()
{
        Foo<std::string> f;
        std::cout << f.t;
        return 0;

        What happen if I declare std::wstring instead of std::string?  C++
Compiler will fail to compile because ‘L’ is not added before string
”Hello World!\n”.
        I do not want to create two separate classes.  One is for string and
another one is for wstring.  I add preprocessor condition.
#if !defined( UNICODE )
        template< typename T >
        std::string Foo<T>::Text = "Hello World!\n";
#else
        template< typename T >
        std::wstring Foo<T>::wText = L"Hello World!\n";
#endif
        Do you have the solution?

Yes, you need std::wcout to << std::wstring.

Generally, it is bad idea to slice your code with preprocessor like
that. Better stick with explicit wchar_t or char. Microsoft has done
that thing with their TCHAR and despite tremendous manpower that
company has their solution outright leaks memory, looks like crap and
is very inefficient when compiled without unicode character set.- Hide quoted text -

I agree with you. Preprocessor condition should be avoided because
you might make mistakes to code C++ in error. For example, UNICODE is
set to true, then C++ Compiler compiles code without error, but it
won’t check non-UNICODE code unless you set UNICODE to false.
I assume Microsoft wants to be perfect. How do Mac OSX, Linux, and
other platforms disagree with Microsoft Specification to use UNICODE
preprocessor?
Generic class is very flexible. Do you agree? How do I believe that
Microsoft succeeds Windows 7? Mac OSX and Linux are behind.

You show code below.

template< typename T >
const T Foo<T>::t = "Hello World!\n";

template<>
const std::wstring Foo<std::wstring>::t = L"2Hello World!\n";

Why not use my code below instead?

template<>
const std::string Foo<std::string>::t = "Hello World!\n";

template<>
const std::wstring Foo<std::wstring>::t = L"2Hello World!\n";

Is my code legal? I do not need to insert typename T into
template<>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top