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?
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?