Visual C++ 2010, rvalue reference bug?

前端 未结 2 1319
自闭症患者
自闭症患者 2021-01-06 05:42

Is it a bug in Visual C++ 2010 or right behaviour?

template
T f(T const &r)
{
    return r;
}

template
T f(T &&r)
         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-06 06:28

    Your fix doesn't solve the problem with static_assert firing though. The static_assert(false, ...) will still trigger for compilers that parse templates at definition time (most do).

    They will see that any function template instantiation will be ill-formed, and the Standard allows them to issue an error for the template itself then, and most will do so.

    For making this work you need to make the expression dependent so that the compiler doesn't know when it parses the template that it will always evaluate to false. For example

    template struct false_ { static bool const value = false; };
    
    template
    T f(T &&r)
    {
        static_assert(false_::value, "no way"); //< line # 10
        return r;
    }
    

提交回复
热议问题