constexpr strange behavior on strings

constexpr strange behavior on strings

I've found that ICC works strange on string when function is constexpr.

#include <tuple>
using namespace std;
constexpr int str_len(const char *s) {
 return *s ? 1 + str_len(s + 1) : 0;
}
int main() {
 constexpr int i = str_len("");
tuple<int, int> t;
 get<i>(t) = 1; /// let's set first element of tuple to 1
}

error: expression must have a constant value
get<i>(t) = 1; /// let's set first element of tuple to 1

If we change str_len to this:

constexpr int str_len(const char *s) {
return 0;
}

Then the all is ok. It seems that ICC doesn't support actions on strings in compile time at all. Will it be fixed? GCC correctly works on this.

ICC 13 update 1 on win.

6 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

Hello,

thank you for reporting this. I've forwarded the problem description to engineering (DPD200240624) and let you know once fixed.

Edit:
As documented we currently support constexpr only partially -- please see here:
http://software.intel.com/en-us/articles/c0x-features-supported-by-intel...

Best regards,

Georg Zitzlsberger

Hi everybody, I did a very quick test and, unfortunately, I couldn't even compile the test case. It fails on:
...
get( t ) = 1;
...
I'll try to spend more time later.

However, the str_len function is a classic example of a recursion ( I did small modifications ):
...
constexpr int str_len( const char *s )
{
return ( constexpr & )( *s != 0x0 ) ? ( 1 + str_len( s + 1 ) ) : 0;
}
...
and because of this it looks like Intel C++ compiler doesn't assume that expression ( 1 + str_len( s + 1 ) is a constant. If s = "12345" it will call itself 5 times and it will return 5 values, that is 1, 2, 3, 4 and 5 ( 5 is the length of the input string ).

Hi man.

But function result is constant and known at compile time.

So it's compiler mistake.

>>...
>>But function result is constant and known at compile time.
>>
>>So it's compiler mistake.

Nobody argues here about the problem and Georg already confirmed it. Thanks for your time.

Hello,

the next minor version (Composer XE 2013 SP1) will have (full) support for that. This version is currently in BETA phase and will be available by end of this year (2013).

Thank you for bringing this up!

Best regards,

Georg Zitzlsberger

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!