C ++通用引用参数不绑定到我的变量

时间:2015-01-03 22:22:22

标签: c++ c++11 rvalue-reference perfect-forwarding

我写了一段用于测试的C ++代码,它非常难看(?),使用灵活的数组,但这不是问题......我在模板中读到了这些,参数如T&&可以绑定到任何内容,例如MyData&MyData&&const MyData&

但在下文中,即使我发送了一个MyData对象,我认为该对象应该被推断为向MyData&函数发送create_variable

这是功能模板:

template <typename T>
Variable<T>& create_variable(Variable<T>* next, T&& data) {
    auto buf = (Variable<T>*) new char[sizeof(Variable<T>) + sizeof(data)];
    buf->_next = next;
    uintptr_t buf_addr = (uintptr_t) ((char*)buf + sizeof(Variable<T>));
    assert(buf_addr % alignof(T) == 0);
    new ((void*)(buf->_data_buf)) T(std::forward<T>(data));
    return *buf;
}

这就是电话:

Variable<MyData>& v = create_variable<MyData>(nullptr, m);

完整的程序:

#include <new>
#include <iostream>
#include <memory>
#include <stdint.h>
#include <assert.h>

template <typename T>
struct Variable {
    struct Variable<T>* _next;
    T _data_buf[];

    Variable() = delete;

    Variable(Variable<T>&& other) = delete;

    T get_data() {
        return _data_buf[0];
    }

    void print() {
        _data_buf[0].print();
    }
};

/// Create a Variable struct with both a next pointer and an inner T data
template <typename T>
Variable<T>& create_variable(Variable<T>* next, T&& data) {
    auto buf = (Variable<T>*) new char[sizeof(Variable<T>) + sizeof(data)];
    buf->_next = next;
    uintptr_t buf_addr = (uintptr_t) ((char*)buf + sizeof(Variable<T>));
    assert(buf_addr % alignof(T) == 0);
    new ((void*)(buf->_data_buf)) T(std::forward<T>(data));
    return *buf;
}
/// Create a Variable struct without the inner T data
template <typename T>
Variable<T>& create_variable(Variable<T>* next) {
    auto buf = (Variable<T>*) new char[sizeof(Variable<T>)];
    buf->_next = next;
    return *buf;
}

/// Test data-type
struct MyData {
    long health;
    long mana;
    MyData (const MyData& other) : health(other.health), mana(other.mana) {}
    MyData (MyData&& other) : health(other.health), mana(other.mana) {}
    MyData(int h, int m) : health(h), mana(m) {}
    void print() { std::cout << "Data: " << health << ',' << mana << ',' << std::endl; }
};

int main() {
    std::cout << "Sizeof Variable<MyData> = " << sizeof(Variable<MyData>) << std::endl;
    MyData m(1, 2);
    Variable<MyData>& v = create_variable<MyData>(nullptr, m);
    v.print();
}

我从clang那里得到错误:

main.cpp:9:7: warning: flexible array members are a C99 feature [-Wc99-extensions]

    T _data_buf[];

      ^

main.cpp:9:7: warning: flexible array members are a C99 feature [-Wc99-extensions]

main.cpp:43:47: note: in instantiation of template class 'Variable<MyData>' requested here    

        std::cout << "Sizeof Variable<MyData> = " << sizeof(Variable<MyData>) << std::endl;

                                                 ^

main.cpp:45:24: error: no matching function for call to 'create_variable'

        Variable<MyData>& v = create_variable<MyData>(nullptr, m);

                              ^~~~~~~~~~~~~~~~~~~~~~~

main.cpp:20:14: note: candidate function [with T = MyData] not viable: no known conversion from 'MyData' to 'MyData &&' for 2nd argument

Variable<T>& create_variable(Variable<T>* next, T&& data) {

         ^

main.cpp:29:14: note: candidate function template not viable: requires single argument     'next', but 2 arguments were provided

Variable<T>& create_variable(Variable<T>* next) {

         ^

2 warnings and 1 error generated.

0 个答案:

没有答案