admin管理员组文章数量:1386710
I have the following code from Ben Deane talk.
#define CX_VALUE(...) [] { \
struct { \
constexpr auto operator()() const noexcept { return __VA_ARGS__; } \
using cx_value_tag = void; \
} val; \
return val; \
}()
template <typename T>
concept cx_value = requires { typename T::cx_value_tag; };
auto func(cx_value auto x) {
constexpr auto val = x();
std::cout << "Constexpr value: " << val.val() << '\n';
}
class S {
public:
constexpr S(const int val) : val_(val){}
int val() const {
return val_;
}
private:
int val_;
};
int main() {
constexpr S non_structural_value{420};
func(CX_VALUE(non_structural_value));
}
Issue is that clang rejects it, gcc accepts it.
Which compiler is right(or is it maybe underspecified in the standard)?
I have the following code from Ben Deane talk.
#define CX_VALUE(...) [] { \
struct { \
constexpr auto operator()() const noexcept { return __VA_ARGS__; } \
using cx_value_tag = void; \
} val; \
return val; \
}()
template <typename T>
concept cx_value = requires { typename T::cx_value_tag; };
auto func(cx_value auto x) {
constexpr auto val = x();
std::cout << "Constexpr value: " << val.val() << '\n';
}
class S {
public:
constexpr S(const int val) : val_(val){}
int val() const {
return val_;
}
private:
int val_;
};
int main() {
constexpr S non_structural_value{420};
func(CX_VALUE(non_structural_value));
}
Issue is that clang rejects it, gcc accepts it.
Which compiler is right(or is it maybe underspecified in the standard)?
Share Improve this question asked Mar 18 at 17:03 NoSenseEtAlNoSenseEtAl 30.3k32 gold badges149 silver badges322 bronze badges 2 |1 Answer
Reset to default 4This has nothing to do with the properties of the class S
.
The problem can be reduced to
struct S {};
int main() {
constexpr S s{};
[]{
struct {
constexpr auto operator()() { return s; }
} val;
};
}
The problem is that s
is being odr-used here in return s;
because it doesn't have the lvalue-to-rvalue conversion applied to it (because it is of class type). And s
is not odr-usable in this scope.
For the same reason you would need to capture s
if you tried to write []{ return s; }
instead. But in the case above capturing won't help either, s
will still not be odr-usable in the scope of the local class.
Clang is correct.
本文标签: language lawyerShould this example for non structural constexpr C argument compileStack Overflow
版权声明:本文标题:language lawyer - Should this example for non structural constexpr C++ argument compile? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744500277a2609282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
non_structural_value
declaration and thus throw an error. – kiner_shah Commented Mar 19 at 6:52