Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

constexpr strange behavior on strings

Bert_Jonson
Beginner
299 Views

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

[cpp]

#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
}

[/cpp]

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:

[cpp]

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

[/cpp]

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.

0 Kudos
5 Replies
Georg_Z_Intel
Employee
299 Views
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-c-compiler Best regards, Georg Zitzlsberger
0 Kudos
SergeyKostrov
Valued Contributor II
299 Views
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 ).
0 Kudos
Bert_Jonson
Beginner
299 Views

Hi man.

But function result is constant and known at compile time.

So it's compiler mistake.

0 Kudos
SergeyKostrov
Valued Contributor II
299 Views
>>... >>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.
0 Kudos
Georg_Z_Intel
Employee
299 Views
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
0 Kudos
Reply