【问题标题】:Variadic template iteration and GCC可变参数模板迭代和 GCC
【发布时间】:2015-11-25 10:37:39
【问题描述】:

我根据wiki实现了一个可变参数模板函数。并在“用函数的“终止版本”重载”的帮助下对其进行迭代。代码:

void writeValue(QDataStream& /*data*/) {}

template<typename A, typename... Values>
void writeValue(QDataStream& data, const A& arg1, const Values&... args)
{
    data << arg1;
    writeValue(data, args...);
}

template<typename... Values>
quint32 PrepareMessage(QDataStream& data, func_code fcode, Values... parameters)
{
  data << quint32(fcode);
  writeValue(data, parameters...);
  return 0;
}

它可以通过 MSVC2013 64 位工具链在 Qt 5.5 for Windows 上毫无问题地构建和使用。 现在我正在尝试使用 Qt 5.5 for Linux over GCC 64 位在 Linux 上构建相同的代码,并在编译时遇到以下错误:

g++ -c -pipe -std=c++11 -g -Wall -W -D_REENTRANT -fPIC -D_64bit -DQT_NETWORK_LIB -DQT_CORE_LIB -I../Trans2QuikWrapper -I. -I../../../Qt/5.5/gcc_64/include -I../../../Qt/5.5/gcc_64/include/QtNetwork -I../../../Qt/5.5/gcc_64/include/QtCore -I. -I../../../Qt/5.5/gcc_64/mkspecs/linux-g++ -o moc_T2Q_Client.o moc_T2Q_Client.cpp
g++ -Wl,-rpath,/home/truf/Qt/5.5/gcc_64 -Wl,-rpath,/home/truf/Qt/5.5/gcc_64/lib -o t2q T2Q_Client.o main_client.o moc_T2Q_Client.o   -L/home/truf/Qt/5.5/gcc_64/lib -lQt5Network -lQt5Core -lpthread 
main_client.o: In function `int QGenericAtomicOps<QBasicAtomicOps<4> >::load<int>(int const&)':
/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: multiple definition of `writeValue(QDataStream&)'
Makefile:192: recipe for target 't2q' failed
T2Q_Client.o:/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: first defined here
moc_T2Q_Client.o: In function `int QGenericAtomicOps<QBasicAtomicOps<4> >::load<int>(int const&)':
/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: multiple definition of `writeValue(QDataStream&)'
T2Q_Client.o:/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: first defined here
collect2: error: ld returned 1 exit status
make: *** [t2q] Error 1

gcc 版本是 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2)。不涉及 Wine - 只是一个文件夹位置。

那里是否支持可变参数模板?需要任何额外的编译参数吗?还是代码中的问题?

【问题讨论】:

  • 你可以在没有“递归”的情况下实现这个:gist.github.com/anonymous/5d766dc156529d0fd49d
  • 这与可变参数模板无关。这是一个在哪里定义函数的简单问题。
  • @Simple: 或在 C++17 中:(data &lt;&lt; ... &lt;&lt; args);.

标签: c++ qt gcc variadic-templates


【解决方案1】:

错误信息说:

`writeValue(QDataStream&)'的多重定义

要解决这个问题,您必须将writeValue() 声明为inline

inline void writeValue(QDataStream& /*data*/) {}

当您在头文件中定义函数时,您始终应该将其标记为inline。这样,如果您在多个翻译单元中 include 标头,它不会破坏 ODR

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多