简体   繁体   English

引用仅在constexpr /编译时上下文中有用吗?

[英]Are references helpful when operating in a constexpr/compile-time context only?

I am exploring the world of constexpr and have decided to create a class that should only be used in constexpr context and other compile-time constructs. 我正在探索constexpr的世界,并决定创建一个仅应在constexpr上下文和其他编译时构造中使用的类。

Usually, I take great care to provide all necessary overloads a class may need, for example: 通常,我会非常小心地提供类可能需要的所有必要的重载,例如:

template <typename T>
struct Thing
{
    Thing(T value) : m_value(value) {}

    T &value() & { return m_value; }
    const T &value() const & { return m_value; }

    T &&value() && { return std::move(m_value); }

private:
    T m_value;
};

The set of overloads for Thing::value should take care of efficient access to the stored value, no unnecessary copies are made. Thing::value的重载集应注意对存储值的有效访问,不会产生不必要的副本。 If the Thing instance is a temporary, the stored value can even be moved out. 如果Thing实例是临时实例,则存储的值甚至可以移出。

But what if Thing is only to be used as a constexpr type, are all these different overloads for Thing::value required or even helpful at all? 但是,如果Thing仅用作constexpr类型,那么Thing::value所有这些不同的重载是必需的,甚至根本没有帮助吗? Or would the following be equivalent: 或等同于以下内容:

template <typename T>
struct Thing
{
    constexpr Thing(T value) : m_value(value) {}

    constexpr T value() const;

private:
    T m_value;
};

My question basically boils down to: are references helpful (more efficient) when operating in a constexpr/compile-time context only; 我的问题基本上可以归结为:仅在constexpr /编译时上下文中操作时,引用是否有用(更有效)? or is passing everything by value equivalent? 还是通过价值等同传递一切?

My question basically boils down to: are references helpful (more efficient) when operating in a constexpr/compile-time context only; 我的问题基本上可以归结为:仅在constexpr /编译时上下文中操作时,引用是否有用(更有效)? or is passing everything by value equivalent? 还是通过价值等同传递一切?

It's a matter of what's your actual problem and how you plan to solve it. 这取决于您的实际问题以及计划如何解决。 In most of the cases (all of them?) you won't need to use references in such a context, I agree, but you can still use them if required. 我同意,在大多数情况下(全部?),您不需要在这种情况下使用引用,但是如果需要,您仍然可以使用它们。

Here as a minimal, working example: 这里是一个最小的工作示例:

const int i = 0;

template <typename T>
struct Thing {
    constexpr Thing(const T &value) : m_value(value) {}
    constexpr const T & value() const { return m_value; }

private:
    const T & m_value;
};

int main() {
    static_assert(Thing<int>{i}.value() == 0, "!");
}

See it up and running on wandbox . 看到它并在wandbox上运行。

So, are references helpful (more efficient) in this case? 因此,在这种情况下引用是否有用(更有效) Well, it's not a matter of efficiency or whatever. 好吧,这不是效率问题。 To use references in such a context you have to have a good reason and the language sets a lot of limitations. 要在这种情况下使用引用,您必须有充分的理由,并且该语言设置了很多限制。 They solve a specific problem, it's not your taste to decide to use references. 他们解决了一个特定的问题,决定使用引用不是您的喜好。
If your problem requires you to use references, they are there for you (and please, contact me - I'm just curious to know what's that problem!). 如果您的问题需要您使用参考,那么它们就在您的身边(请与我联系-我很好奇知道这是什么问题!)。 Otherwise feel free to keep on with passing by value. 否则,请随时通过价值传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM