mip
mip

Reputation: 8713

Function with pointer to a function and vector with default value as arguments results in compilation error

Why following code does not compile on MSVC

#include <vector>

void func(double (* fptr)(double), const std::vector<double> & v = {})
{
}

I'm getting following errors.

source_file.cpp(6): error C2065: 'fptr': undeclared identifier

source_file.cpp(6): error C2062: type 'double' unexpected

source_file.cpp(6): error C2143: syntax error: missing ';' before '{'

source_file.cpp(6): error C2143: syntax error: missing ')' before ';'

source_file.cpp(6): error C2447: '{': missing function header (old-style formal list?)

source_file.cpp(6): error C2059: syntax error: ')'

source_file.cpp(7): error C2447: '{': missing function header (old-style formal list?)

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

When I remove either - vector default value:

void func(double (* fptr)(double), const std::vector<double> & v)

or the function pointer:

void func(const std::vector<double> & v = {})

errors disappear. Is it some MSVC bug or am I missing something?

Clang and G++ have no problems with the code.

You can check out the code with https://rextester.com/l/cpp_online_compiler_visual

Upvotes: 2

Views: 72

Answers (1)

Is it some MSVC bug or am I missing something?

You miss nothing. It's a MSVC bug. You can work around it by overloading:

void func(double (* fptr)(double), const std::vector<double> & v)
{
}

void func(double (* fptr)(double)) {
    std::vector<double> v;
    func(fptr, v); // or just func(fptr, {})
}

Though it's worth noting that taking the address of func is now ambiguous, unlike in your original, perfectly standard conforming code.

Upvotes: 4

Related Questions