0

Please help me with this error.

I am trying to make a sample that explains constexpr keyword in c++. I am using Visual Studio 2013. Following is the code of my cpp file.

#include <iostream>
#include <stdexcept>

const int sampleconstant = 5;


constexpr int constTest(void)
{
    return sampleconstant;
}
int main()
{

    std::cout << constTest();
    getchar();
    return 0;
}

This shows compile time error as follows:

Error 1 error C2144: syntax error : 'int' should be preceded by ';'

Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I may be doing something silly here. But really can't figure out this error. Code looks ok to me. If any one can help me with error please help.

techfun
  • 913
  • 2
  • 13
  • 25
  • I think maybe you misunderstood the meaning of `constexpr` ... it means that the value of the function can be calculated at compile time. This is clearly not the case for `factorial`, since the value of `n` is not fixed at compile time. See http://en.wikipedia.org/wiki/C%2B%2B11#constexpr_.E2.80.93_Generalized_constant_expressions – Markku K. Jan 16 '14 at 18:31
  • Hi Markku K. Thanks for the view. But I simplified the code above. The above modified code is showing same error. I think my understandings are ok about constexpr. But the above modified code is showing same error. Let me know if you still find some problem in code – techfun Jan 16 '14 at 18:42
  • **This version of your compiler does not support that feature** (and a quick search on Google or even SO would have told you that!). – Lightness Races in Orbit Jan 16 '14 at 18:43
  • 3
    @MarkkuK.: Not true (unless there was an edit in the grace period I didn't see?) - with the argument to `factorial` being the integer literal `4`, his code was absolutely fine. – Lightness Races in Orbit Jan 16 '14 at 18:44
  • @dyp: Tee hee hee. Anyway he said "Visual Studio 2013"; until he changes that... – Lightness Races in Orbit Jan 16 '14 at 18:48
  • @LightnessRacesinOrbit. Thanks a lot. You saved my time. This was a new topic for me. So I was confused. – techfun Jan 16 '14 at 18:49
  • @LightnessRacesinOrbit, quite true, I was the one who misunderstood `constexpr`! – Markku K. Jan 16 '14 at 18:49

1 Answers1

-3

As already told you, it is not supported on VS.

You can see at this link a list of featured supported by the compilers: http://wiki.apache.org/stdcxx/C++0xCompilerSupport

SomaOS
  • 51
  • 4
  • 5
    -1: That table is horrifically out of date. For one thing, it hasn't been "C++0x" for _three years_. For another, the data hasn't been updated since May last year. Finally, the feature _is_ supported by Visual Studio, just not the version that the OP is using. – Lightness Races in Orbit Jan 16 '14 at 18:53
  • I'm sorry for the fault, I googled to find this table and I didn't realized that was out of date. – SomaOS Jan 16 '14 at 19:39