constexpr strange behavior on strings

constexpr strange behavior on strings

Portrait de Bert Jonson

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 / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.
Portrait de Georg Zitzlsberger (Intel)

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

Portrait de Sergey Kostrov

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 ).

Portrait de Bert Jonson

Hi man.

But function result is constant and known at compile time.

So it's compiler mistake.

Portrait de Sergey Kostrov

>>...
>>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.

Portrait de Georg Zitzlsberger (Intel)

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

Connectez-vous pour laisser un commentaire.