I have some C ++ 11 code which I am trying to connect to Visual C ++ Compiler 2015. The source code works fine, however I need to rewrite it to solve problems using constexpr.
Source code (simplified example)
#include <iostream>
struct String
{
static constexpr const char * value{ "STRING" };
};
template<typename Base>
class Derived
{
public:
static constexpr const char * value{ Base::value };
};
template<typename BarType>
struct Foo
{
static constexpr const char * value{ BarType::value };
};
using Bar = Derived<String>;
using FooBar = Foo<Bar>;
int main()
{
std::cout << "FooBar::value = " << FooBar::value << std::endl;
}
Fingerprints:
FooBar::value = STRING
However, when I rewrite, some static variables are not initialized. Despite the fact that it just compiles.
Ported Code (not working)
#include <iostream>
struct String
{
static const char * value;
};
const char * String::value = "STRING";
template<typename Base>
class Derived
{
public:
static const char * value;
};
template<typename Base>
const char * Derived<Base>::value = { Base::value };
template<typename BarType>
struct Foo
{
static const char * value;
};
template<typename BarType>
const char * Foo<BarType>::value = { BarType::value };
using Bar = Derived<String>;
using FooBar = Foo<Bar>;
int main()
{
std::cout << "FooBar::value = " << FooBar::value << std::endl;
}
Fingerprints:
This can be reproduced in Clang and Visual-C ++, however GCC FooBar::value = STRING
also prints in the second example.
Update: Working Solution
@serge-ballesta. , . , constexpr VS.