Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Ian_Mallett1
Beginner
43 Views

Bug: constexpr fails on a constant

See the following example:

#include <climits>
#include <cstddef>
#include <type_traits>

template <int n> class MyClass {
	public:
		//See: http://stackoverflow.com/a/21298525/688624
		template <typename T, typename=typename std::enable_if<std::is_integral<T>::value>::type/*,typename=typename std::enable_if<std::is_unsigned<T>::value>::type*/>
		static constexpr T get_next_pot(T value, size_t maxb=sizeof(T)*CHAR_BIT,size_t curb=1) {
			return maxb<=curb ? value : get_next_pot( ((value-1) | ((value-1)>>curb))+1, maxb, curb<<1 );
		}

	public:
		enum {
			NEXT_POT = get_next_pot(n)
		};
};

int main(void) {
	MyClass<6> mat;
	return 0;
}

This produces:

1>..\main.cpp(15): error : function call must have a constant value in a constant expression
1>                NEXT_POT = get_next_pot(n)
1>                           ^
1>            detected during instantiation of class "MyClass<n> [with n=6]" at line 20

MSVC, Clang, and GCC all compile it fine. Moving the function "get_next_pot(...)" outside of the class causes it to compile correctly.

0 Kudos
1 Reply
Kittur_G_Intel
Employee
43 Views

Thanks for catching this bug, Ian. I could reproduce the issue and will with the developers. Appreciate for letting us know and for your patience through this. I'll keep you updated on the issue progress, thanks.

Kittur