c++ generic function extension question

Kenny Simpson theonetruekenny@gmail.com
Sat May 16 04:49:00 GMT 2015


4.9 introduced unconstrained generic functions as an extension:

https://gcc.gnu.org/gcc-4.9/changes.html
"G++ supports unconstrained generic functions as specified by §4.1.2
and §5.1.1 of N3889: Concepts Lite Specification"

And in that spec, 5.1.1 says:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3889.pdf
15 "A generic function is denoted by function declarator having auto
or a concept-name as part of the type-specifier in its
parameter-declaration-clause"

17 "All placeholder types introduced using the same concept-name have
the same invented template parameter."

If I write a generic function using two 'auto' type-specifiers, they
seem to become the same invented template parameter as if they were
concept names instead.:

auto add(auto a, auto b) { return a + b; }

add(3, 4.5)  gives me 7
add(3.5, 4)  gives me 7.5

sample program:
#include <iostream>

auto add_1(auto a, auto b) { return a + b;}
auto add_2 = [](auto a, auto b) { return a + b;};

int main()
{
  std::cout
    << "a1: " << add_1(3.5, 4) << "\n"
    << "a2: " << add_1(3, 4.5) << "\n"
    << "a3: " << add_2(3.5, 4) << "\n"
    << "a4: " << add_2(3, 4.5) << "\n";
}

This gives me:
a1: 7.5
a2: 7
a3: 7.5
a4: 7.5

This is with a fedora 22/x86-64 machine.
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --disable-libgcj
--with-default-libstdcxx-abi=c++98 --with-isl --enable-libmpx
--enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 5.1.1 20150422 (Red Hat 5.1.1-1) (GCC)

Should I open a bug report or try again with a recent snapshot?

thanks,
-Kenny



More information about the Gcc-help mailing list