Bogus error regarding returning the address of or a reference to a temporary when attempting aggregate initialization inside of a constexpr function - by ildjarn

Status : 


Sign in
to vote
ID 1498733 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 7/2/2015 9:49:59 AM
Access Restriction Public


Given the following code:

////////////////// CODE BEGIN //////////////////
#include <cstddef>
#include <type_traits>
#include <array>
#include <iostream>

template<typename T, T... Ns>
inline constexpr std::array<T, sizeof...(Ns)> make_squares_impl(std::integer_sequence<T, Ns...>) noexcept
    return {{ (Ns * Ns)... }};

template<typename T, std::size_t SizeN>
inline constexpr std::array<T, SizeN> make_squares() noexcept
    return make_squares_impl<T>(std::make_integer_sequence<T, SizeN>{});

int main()
    constexpr auto squares = make_squares<unsigned, 20>();
    for (unsigned i{}; i != 20; ++i)
        std::cout << i << ":\t" << squares[i] << '\n';
////////////////// CODE END //////////////////

The code should compile cleanly, and does with current versions of Clang with libc++, but gives the following errors with VC++ v19.00.23015.0(x86):

main.cpp(15): error C2131: expression did not evaluate to a constant
main.cpp(9): note: failure was caused by returning the address of or a reference to a temporary
main.cpp(15): note: while evaluating 'make_squares_impl(integer_sequence<unsigned int,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19>{})'
main.cpp(20): note: while evaluating 'make_squares()'
Sign in to post a comment.
Posted by Microsoft on 9/2/2015 at 2:42 PM
Thank you for reporting the bug. The issue has now been fixed for Update 1.

Tanveer Gani
Visual C++ Team.
Posted by ildjarn on 7/20/2015 at 11:02 PM
I can confirm that this problem is still present in VC++ 2015 RTM (v19.00.23026) and v19.00.23106.
Posted by Microsoft on 7/2/2015 at 11:05 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If you require immediate assistance with this issue, please contact product support at