0

Here is a simple class Test :

#pragma once
class Test
{
public:
    constexpr Test() : i(0) {}
    constexpr Test(int i) : i(i) {}

private:
    int i;
};

and I when I execute this main :

#include <iostream>
#include "Test.h"

int main()
{
    constexpr Test t(2);
    std::cin.get();
}

everything compiles perfectly. But when I move the implementation of both constructors of Test to a source file Test.cpp like so :

#include "Test.h"

constexpr Test::Test() : i(0)
{
}

constexpr Test::Test(int i) : i(i)
{
}

I've got this following error message :

Error C2127 't': illegal initialization of 'constexpr' entity with a non-constant expression

What's wrong here? (I am using Visual Studio 2015)

Corvinus
  • 169
  • 2
  • 10
  • @LogicStuff It's a different error in this case. (Why is this a duplicate)? –  Aug 01 '16 at 21:45
  • @VeniVidiVici presumably because OP is getting the error trying to define a constexpr constructor in a different file. The dupe target explains that you can't do that. – jaggedSpire Aug 01 '16 at 21:53

0 Answers0