[llvm-bugs] [Bug 30560] New: std::bind does invoke constexpr functions two times with different parameter types

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 29 04:48:19 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=30560

            Bug ID: 30560
           Summary: std::bind does invoke constexpr functions two times
                    with different parameter types
           Product: libc++
           Version: 3.9
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: benni.buch at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

My example is based on https://llvm.org/bugs/show_bug.cgi?id=23141

//--------> Code
#include <functional>
#include <type_traits>


struct Foo{
    template < typename U >
    void operator()(U&& u)const{
        static_assert(std::is_same< U, int& >::value, "");
    }
};

struct Bar{
    template < typename U >
    constexpr void operator()(U&& u)const{
        static_assert(std::is_same< U, int& >::value, "");
    }
};


int main(){
    std::bind(Foo(), 42)(); // OK without constexpr
    std::bind(Bar(), 42)(); // Error with constexpr
}
//<-------- Code End

>$ clang++ -stdlib=libc++ -std=c++1z -o test test.cpp 
-------> Output
test.cpp:15:3: error: static_assert failed ""
                static_assert(std::is_same< U, int& >::value, "");
                ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4287:23:
note: in instantiation of function template specialization
'Bar::operator()<const int &>' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
                      ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4304:9:
note: while substituting deduced template arguments into function template
'__invoke' [with _Fp = const Bar, _Args = <const int &>]
        _VSTD::__invoke(_VSTD::declval<_Fp>(), _VSTD::declval<_Args>()...));
        ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2149:31:
note: in instantiation of template class 'std::__1::__invokable_r<void, const
Bar, const int &>' requested here
    static const bool value = __invokable<_Fp,
                              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2154:18:
note: in instantiation of template class
'std::__1::__is_valid_bind_return<const Bar, const std::__1::tuple<int>,
std::__1::tuple<> >' requested here
          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
                 ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2260:18:
note: in instantiation of default argument for '__bind_return<const Bar, const
std::__1::tuple<int>, std::__1::tuple<> >' required here
        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:22:22: note: while substituting deduced template arguments into
function template 'operator()' [with _Args = <>]
        std::bind(Bar(), 42)(); // Error with constexpr
                            ^
1 error generated.
<------- Output End


The problem is that '_Args' is 'const int &', but it should be 'int&'. If I
force the static_assert to fail by checking if 'U' equals 'void', I get two
different substituten paths:

//--------> Code
#include <functional>
#include <type_traits>


struct Bar{
    template < typename U >
    constexpr void operator()(U&& u)const{
        static_assert(std::is_same< U, void >::value, "");
    }
};


int main(){
    std::bind(Bar(), 42)();
}
//<-------- Code End

>$ clang++ -stdlib=libc++ -std=c++1z -o test test.cpp 
-------> Output
test.cpp:9:3: error: static_assert failed ""
                static_assert(std::is_same< U, void >::value, "");
                ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4287:23:
note: in instantiation of function template specialization 'Bar::operator()<int
&>' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
                      ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4304:9:
note: while substituting deduced template arguments into function template
'__invoke' [with _Fp = Bar, _Args = <int &>]
        _VSTD::__invoke(_VSTD::declval<_Fp>(), _VSTD::declval<_Args>()...));
        ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2142:31:
note: in instantiation of template class 'std::__1::__invokable_r<void, Bar,
int &>' requested here
    static const bool value = __invokable<_Fp,
                              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2154:18:
note: in instantiation of template class 'std::__1::__is_valid_bind_return<Bar,
std::__1::tuple<int>, std::__1::tuple<> >' requested here
          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
                 ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2251:18:
note: in instantiation of default argument for '__bind_return<Bar,
std::__1::tuple<int>, std::__1::tuple<> >' required here
        typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:15:22: note: while substituting deduced template arguments into
function template 'operator()' [with _Args = <>]
        std::bind(Bar(), 42)();
                            ^
test.cpp:9:3: error: static_assert failed ""
                static_assert(std::is_same< U, void >::value, "");
                ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4287:23:
note: in instantiation of function template specialization
'Bar::operator()<const int &>' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
                      ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/type_traits:4304:9:
note: while substituting deduced template arguments into function template
'__invoke' [with _Fp = const Bar, _Args = <const int &>]
        _VSTD::__invoke(_VSTD::declval<_Fp>(), _VSTD::declval<_Args>()...));
        ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/__config:451:15: note:
expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2149:31:
note: in instantiation of template class 'std::__1::__invokable_r<void, const
Bar, const int &>' requested here
    static const bool value = __invokable<_Fp,
                              ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2154:18:
note: in instantiation of template class
'std::__1::__is_valid_bind_return<const Bar, const std::__1::tuple<int>,
std::__1::tuple<> >' requested here
          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
                 ^
/media/data/programme/llvm-3.9.0/bin/../include/c++/v1/functional:2260:18:
note: in instantiation of default argument for '__bind_return<const Bar, const
std::__1::tuple<int>, std::__1::tuple<> >' required here
        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:15:22: note: while substituting deduced template arguments into
function template 'operator()' [with _Args = <>]
        std::bind(Bar(), 42)();
                            ^
2 errors generated.
<------- Output End


I'm working with the standard release of LLVM 3.9.0 from llvm.org.

clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /media/data/programme/llvm-3.9.0/bin

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160929/039df7b1/attachment.html>


More information about the llvm-bugs mailing list