arrays - MSVC rejects the syntax of a line which GCC accepts - makes weird complaint - Stack Overflow

Consider the following C++11 code:template <typename T>class optional_ref {public:optional_ref

Consider the following C++11 code:

template <typename T>
class optional_ref {
public:
    optional_ref() : ref_(nullptr) {}
    optional_ref(const T& ref) : ref_(&ref) {}

    bool has_value() const { return ref_ != nullptr; }
    const T& value() const { return *ref_; }

private:
    const T* ref_;
};

class stream_t {};

template <typename T, unsigned long N>
void copy(T(&destination)[N], optional_ref<const stream_t> stream = {}) // !!!
{ destination[0] = 1; }

This code compiles fine with g++; but - MSVC doesn't like it for some reason, giving the following errors regarding the line marked !!!:

error C2065: 'destination': undeclared identifier
error C2275: 'optional_ref<const stream_t>': expected an expression instead of a type
error C2146: syntax error: missing ')' before identifier 'stream'
error C2143: syntax error: missing ';' before '{'
error C2143: syntax error: missing ')' before ';'
error C2447: '{': missing function header (old-style formal list?)
error C2059: syntax error: ')'
error C2447: '{': missing function header (old-style formal list?)

It seems like it is having some trouble with the array-reference parameter. But - what is it actually complaining about?

I would appreciate some input from someone with more MSVC experience, who may be able to better decipher this sequence of errors.

Consider the following C++11 code:

template <typename T>
class optional_ref {
public:
    optional_ref() : ref_(nullptr) {}
    optional_ref(const T& ref) : ref_(&ref) {}

    bool has_value() const { return ref_ != nullptr; }
    const T& value() const { return *ref_; }

private:
    const T* ref_;
};

class stream_t {};

template <typename T, unsigned long N>
void copy(T(&destination)[N], optional_ref<const stream_t> stream = {}) // !!!
{ destination[0] = 1; }

This code compiles fine with g++; but - MSVC doesn't like it for some reason, giving the following errors regarding the line marked !!!:

error C2065: 'destination': undeclared identifier
error C2275: 'optional_ref<const stream_t>': expected an expression instead of a type
error C2146: syntax error: missing ')' before identifier 'stream'
error C2143: syntax error: missing ';' before '{'
error C2143: syntax error: missing ')' before ';'
error C2447: '{': missing function header (old-style formal list?)
error C2059: syntax error: ')'
error C2447: '{': missing function header (old-style formal list?)

It seems like it is having some trouble with the array-reference parameter. But - what is it actually complaining about?

I would appreciate some input from someone with more MSVC experience, who may be able to better decipher this sequence of errors.

Share Improve this question edited Nov 17, 2024 at 21:55 einpoklum asked Nov 16, 2024 at 19:41 einpoklumeinpoklum 133k80 gold badges422 silver badges867 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

This looks like a MSVC bug, you can report it to Microsoft here. In the meantime, aliasing the array as another type works for me:

template <typename T, size_t N>
using dest_array = T[N];

// Function template
template <typename T, size_t N>
void copy(dest_array<T, N>& destination, span<T const> source, optional_ref<const stream_t> stream = {})
{
    ...
}

Link to Godbolt example to verify it works on latest MSVC: https://godbolt./z/nb76MdP4c. Thanks!

This seems to be an MSVC bug, which I have now filed as:

MSVC rejects syntax of reference to C array as a function parameter

Will update if/when it's fixed.

Credit to this answer from Anis Ladram, who suggested this as a compiler bug.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745650431a4638231.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信