Why is the template version allowed to compile in gcc? Is it a compiler bug or is it actually valid when used with templates? Can someone explain this to me, please?
It does not compile on clang or other compilers used on godbolt.org.
The compile errors generates from both string and stringstream being used in a constexpr.
#include <iostream>
#include <string>
#include <sstream>
template<typename T>
constexpr std::string func1(T a, T b) //Compiles and runs
{
std::stringstream ss;
ss << a << b << a+b;
return ss.str();
}
constexpr std::string func2(int a, int b) //Compile error
{
std::stringstream ss;
ss << a << b << a+b;
return ss.str();
}
int main()
{
int a = 5;
int b = 7;
std::cout << func1(a,b) << std::endl;
return 0;
}