Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Compiler bug or not legal C++11 ?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
DomesticatedPotato
Posts: 1
Joined: 2015-03-14 20:15

Compiler bug or not legal C++11 ?

#1 Post by DomesticatedPotato »

The following code (synthetic test case) compiles with clang++, but not g++ (the versions currently in Jessie)

Code: Select all

#include <iostream>

template<typename return_type, typename... argument_types>
return_type invoke(return_type (*func)(argument_types...), argument_types&&... args)
{
        return func(std::forward<argument_types>(args)...);
}

int add(const int i, const int j) { return i + j; }

int main() {
        std::cout << invoke<int, const int, const int>(&add, 4, 5) << std::endl;
}
With g++, I get

Code: Select all

$ g++ -std=c++11 -o test test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:12:59: error: no matching function for call to ‘invoke(int (*)(int, int), int, int)’
  std::cout << invoke<int, const int, const int>(&add, 4, 5) << std::endl;
                                                           ^
test.cpp:12:59: note: candidate is:
test.cpp:4:13: note: template<class return_type, class ... argument_types> return_type invoke(return_type (*)(argument_types ...), argument_types&& ...)
 return_type invoke(return_type (*func)(argument_types...), argument_types&&... args)
             ^
test.cpp:4:13: note:   template argument deduction/substitution failed:
test.cpp:12:59: note:   mismatched types ‘const int’ and ‘int’
  std::cout << invoke<int, const int, const int>(&add, 4, 5) << std::endl;
                                                           ^
clang++ produces a binary that outputs 9, as expected.

After template argument substitution, the first argument of invoke is int(*)(const int, const int), which matches the signature of add exactly (and is equivalent to int(*)(int, int)).

It seems to me like g++ (correctly) drops the const qualifiers from the arguments of add, but fails to drop them from the arguments of the function pointer expected by invoke before matching the signatures of the expected function pointer and the passed-in function pointer.

So the question is in the title. Is this a bug I should report against g++ or am I thinking wrong here?

Post Reply