Create Generic Class with String?

Discussion in 'C++' started by Immortal Nephi, Apr 12, 2010.

  1. 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?
    Immortal Nephi, Apr 12, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Öö Tiib Guest

    On 12 apr, 22:40, Immortal Nephi <> wrote:
    >         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.
    Öö Tiib, Apr 12, 2010
    #2
    1. Advertising

  3. On Apr 12, 4:22 pm, Öö Tiib <> wrote:
    > On 12 apr, 22:40, Immortal Nephi <> wrote:
    >
    >
    >
    >
    >
    > >         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<>.
    Immortal Nephi, Apr 13, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purush
    Replies:
    4
    Views:
    1,605
    Purush Rudrakshala
    Apr 13, 2005
  2. Murat Tasan
    Replies:
    1
    Views:
    8,022
    Chaitanya
    Feb 3, 2009
  3. E11
    Replies:
    1
    Views:
    4,706
    Thomas Weidenfeller
    Oct 12, 2005
  4. Replies:
    2
    Views:
    419
  5. minlearn
    Replies:
    2
    Views:
    444
    red floyd
    Mar 13, 2009
Loading...

Share This Page