So, playing around with constexpr, MSVC (Visual Studio 2012) gave me an error while trying to qualify my function with the constexpr keyword using this simple program (includes omitted):
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n-1));
}
int main(void)
{
const int fact_three = factorial(3);
std::cout << fact_three << std::endl;
return 0;
}
constexpr was underlined red with the following message:
Error : this declaration has no storage class or type specifier
and trying to compile the program gave the following output:
1>main.cpp(5): error C2144: syntax error : 'int' should be preceded by ';'
1>main.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
It really puzzles me as it is the very example that Cppreference uses to illustrate the use of constexpr. At first I used a simple function that returned a literal, i.e. constexpr int func(){return 5;}, but which yielded the same error. I interpreted the first message as "it should be a member function of a struct or class", but the example from Cppreference shows that it's not necessary apparently.
So, what am I obviously missing here ?
Quite simply - because Visual Studio doesn't support constexpr (prior to Visual Studio 2015).
Note that MSVC++11 is Visual Studio 2012; VC++10 is Visual Studio 2010.
Related
This piece of code is compiled with no issues:
const int tmp1 = 1, tmp2 = 1;
const bool cmp = (tmp1 == tmp2);
static_assert(cmp, "OK");
This one is fine, too:
const bool cmp = (HUGE_VALF == HUGE_VALF);
static_assert(cmp, "OK");
This one is not:
const auto tmp = HUGE_VALF;
const bool cmp = (tmp == tmp);
static_assert(cmp, "OK"); // <-- error
Error: expression must have a constant value.
The value of variable "cmp" cannot be used as a constant
Same behaviour with static const.
What is wrong? HUGE_VALF is a preprocessor macro, i.e. constant, and cmp is with no doubt a compile time constant...
Environment:
Microsoft Windows 10
Microsoft Visual C++ 2013 & 2019
With this test program I got to the problem:
#include <cmath>
constexpr auto val1 = 1.23f;
constexpr auto val2 = HUGE_VALF;
static_assert(val1 == val1, "OK");
static_assert(val2 == val2, "OK");
int main()
{
}
Result:
Visual C++ 2019 32 bits: OK
Visual C++ 2019 64 bits: OK
Visual C++ 2013 32 bits: OK
Visual C++ 2013 64 bits: Failed
Output:
1> TestConstexpr.cpp(5): error C2144: syntax error : 'auto' should be preceded by ';'
1> TestConstexpr.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> TestConstexpr.cpp(6): error C2144: syntax error : 'auto' should be preceded by ';'
1> TestConstexpr.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> TestConstexpr.cpp(6): error C2086: 'int constexpr' : redefinition
1> TestConstexpr.cpp(5) : see declaration of 'constexpr'
1> TestConstexpr.cpp(8): error C2057: expected constant expression
1> TestConstexpr.cpp(9): error C2057: expected constant expression
We can declare anonymous struct inside for loop as below (g++):
for(struct { bool OK = true; } s; s.OK; s.OK = false)
std::cout << "Hello, world!\n";
But, this code results in compilation error in MSVC as:
source_file.cpp(7): error C2332: 'struct': missing tag name
source_file.cpp(7): error C2062: type 'bool' unexpected
source_file.cpp(7): error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
How to fix it?
Version:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26430 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
Please not that MSVC 19.14 shows the error C2062: type 'bool' unexpected even for a named struct S as in this example:
for(struct S { bool OK = true; } s; s.OK; s.OK = false)
std::cout << "Hello, world!\n";
Demo: https://godbolt.org/z/Ev1GMGMYd
I think the easiest fix would be to upgrade to the next compiler version MSVC 19.15 where both issues are fixed. Demo: https://godbolt.org/z/W14dvW5eW
I am trying to compile some old C++ in Visual Studio 2008 that compiles in Visual C++ 6. I am trying to deal with the errors through configuration changes and compiler instructions instead of changing the code.
In a lot of places in the code, there are variables and functions declared without a type. The compiler gives an error like "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int".
For example:
class DLL WebPage
{
private:
char *ip_;
char *port_;
char *page_;
public:
WebPage();
~WebPage();
get(char *ip,char *port,char *page); //<---This is the trouble maker
char *ip(void);
char *port(void);
char *page(void);
};
Any way to get this to compile in a newer IDE?
I am using Visual C++ express 2008 try to compile code similar to below:
no problem
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008
It seem like the same code will be compiled with no error/warning on gcc compiler. Why I get this error on VC++? Is it a C/C++ standard way to do casting on struct value? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
Use av_get_time_base_q() instead of AV_TIME_BASE_Q for C++ or VS.
This was fixed in a patch
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options:
Rename your program with a .c suffix
Switch on the /TC flag for the program that does not compile.
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.
For compound-literals errors in C++
wrong:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);
This question already has answers here:
constexpr not compiling in VC2013
(4 answers)
Closed 9 years ago.
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.
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