cosmos icon indicating copy to clipboard operation
cosmos copied to clipboard

Variant.hpp 中功能与书中不一致

Open taoyesheng opened this issue 6 years ago • 2 comments

运行《深入应用C++11代码优化与工程级应用》。中139页的测试列子通过不了。您是否没有上传最新的代码

taoyesheng avatar Mar 19 '18 08:03 taoyesheng

@taoyesheng 具体是哪个接口测试不通过?

qicosmos avatar Mar 28 '18 07:03 qicosmos

@qicosmos 测试不不通过主要是因为在以下语句中:

    typedef Variant<int, double,  string, int> cv;
    cv v = 10;
    ...

得到的错误信息是:

In file included from test.cc:3:0:
Variant.h: In instantiation of ‘void Variant<Types>::destroy(const std::type_index&, void*) [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]’:
Variant.h:128:20:   required from ‘Variant<Types>::Variant(T&&) [with T = int; <template-parameter-2-2> = void; Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]’
test.cc:40:12:   required from here
Variant.h:190:25: error: no match for call to ‘(Variant<Types>::destroy(const std::type_index&, void*) [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]::<lambda(int&&, double&&, std::__cxx11::basic_string<char>&&, int&&)>) (int, int, int, int)’
         [](Types&&...){}((destroy0<Types>(index, buf), 0)...);
                         ^
Variant.h:190:25: note: candidate: void (*)(int&&, double&&, std::__cxx11::basic_string<char>&&, int&&) <conversion>
Variant.h:190:25: note:   candidate expects 5 arguments, 5 provided
Variant.h:190:22: note: candidate: Variant<Types>::destroy(const std::type_index&, void*)::<lambda(Types&& ...)> [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]
         [](Types&&...){}((destroy0<Types>(index, buf), 0)...);
                      ^
Variant.h:190:22: note:   no known conversion for argument 3 from ‘int’ to ‘std::__cxx11::basic_string<char>&&’
Variant.h: In instantiation of ‘void Variant<Types>::move(const std::type_index&, void*, void*) [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]’:
Variant.h:102:13:   required from ‘Variant<Types>::Variant(Variant<Types>&&) [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]’
test.cc:40:12:   required from here
Variant.h:202:25: error: no match for call to ‘(Variant<Types>::move(const std::type_index&, void*, void*) [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]::<lambda(int&&, double&&, std::__cxx11::basic_string<char>&&, int&&)>) (int, int, int, int)’
         [](Types&&...){}((move0<Types>(old_t, old_v, new_v), 0)...);
                         ^
Variant.h:202:25: note: candidate: void (*)(int&&, double&&, std::__cxx11::basic_string<char>&&, int&&) <conversion>
Variant.h:202:25: note:   candidate expects 5 arguments, 5 provided
Variant.h:202:22: note: candidate: Variant<Types>::move(const std::type_index&, void*, void*)::<lambda(Types&& ...)> [with Types = {int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}]
         [](Types&&...){}((move0<Types>(old_t, old_v, new_v), 0)...);
                      ^
Variant.h:202:22: note:   no known conversion for argument 3 from ‘int’ to ‘std::__cxx11::basic_string<char>&&’

如果将语句 typedef Variant<int, double, string, int> cv; 中的 string 删去,则代码可以编译通过,希望作者修一下这个 bug

Phoenix500526 avatar Jan 07 '21 09:01 Phoenix500526