Complex literal 'i' used in function argument

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question


















  • 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















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?







share|improve this question


















  • 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













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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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













  • 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













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.







share|improve this answer






















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.







    share|improve this answer


























      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.







      share|improve this answer
























        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.







        share|improve this answer














        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.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 8 at 9:18

























        answered Aug 8 at 9:12









        VTT

        21.1k32143




        21.1k32143



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            List of Gilmore Girls characters

            What does second last employer means? [closed]

            One-line joke