Complex literal 'i' used in function argument
Clash Royale CLAN TAG#URR8PPP
up vote
20
down vote
favorite
There seems to be a problem, using the literal i
in C++ with std::complex
.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
Do I have to explicitly construct a std::complex
with 1.0i
?
c++ std complex-numbers
add a comment |Â
up vote
20
down vote
favorite
There seems to be a problem, using the literal i
in C++ with std::complex
.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
Do I have to explicitly construct a std::complex
with 1.0i
?
c++ std complex-numbers
3
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
1
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
1
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11
add a comment |Â
up vote
20
down vote
favorite
up vote
20
down vote
favorite
There seems to be a problem, using the literal i
in C++ with std::complex
.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
Do I have to explicitly construct a std::complex
with 1.0i
?
c++ std complex-numbers
There seems to be a problem, using the literal i
in C++ with std::complex
.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
Do I have to explicitly construct a std::complex
with 1.0i
?
c++ std complex-numbers
edited Aug 8 at 14:24


Peter Mortensen
12.9k1983111
12.9k1983111
asked Aug 8 at 9:03


Louen
1,99311430
1,99311430
3
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
1
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
1
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11
add a comment |Â
3
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
1
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
1
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11
3
3
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
1
1
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
1
1
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
24
down vote
accepted
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
24
down vote
accepted
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
add a comment |Â
up vote
24
down vote
accepted
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
add a comment |Â
up vote
24
down vote
accepted
up vote
24
down vote
accepted
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
edited Aug 8 at 9:18
answered Aug 8 at 9:12


VTT
21.1k32143
21.1k32143
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51742673%2fcomplex-literal-i-used-in-function-argument%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Which compiler and version? This compiles for me on GCC trunk, Clang trunk, and a slightly older version of MSVC.
– chris
Aug 8 at 9:06
1
For example, ideone's gcc 6.3, as shown here : ideone.com/qxw5eI
– Louen
Aug 8 at 9:09
1
Somehow it compiles fine on wandbox's gcc 6.3
– VTT
Aug 8 at 9:11