Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用模板函数时未定义的引用_C++_Templates - Fatal编程技术网

C++ 使用模板函数时未定义的引用

C++ 使用模板函数时未定义的引用,c++,templates,C++,Templates,下面的代码被简化为仅显示问题 template <unsigned bits_count, typename ut_t = unsigned short, typename st_t = short, typename udt_t = unsigned, typename sdt_t = int> struct int_t { typedef ut_t ut; ut comp[bits_count / (sizeof(ut) * 8)]; }; templ

下面的代码被简化为仅显示问题

template <unsigned bits_count, typename ut_t = unsigned short, typename st_t = short, typename udt_t = unsigned, typename sdt_t = int>
struct int_t
{
    typedef ut_t     ut;

    ut comp[bits_count / (sizeof(ut) * 8)];
};

template<typename ot, typename it>
inline ot& mathx_int_from_t_to_niv(const it& value, ot& result)
{
    typedef typename it::ut ut;

    result = ot(0);

    if (sizeof(ot) <= sizeof(ut)) return result = ot(value.comp[0]);

    return result = *(ot*)value.comp;
}

template <typename ot, typename it>
ot numeric_cast(const it& value);

template<unsigned bits_count, typename ut_t, typename st_t, typename udt_t, typename sdt_t>
inline int numeric_cast(const int_t<bits_count, ut_t, st_t, udt_t, sdt_t>& value)
{
    typedef int_t<bits_count, ut_t, st_t, udt_t, sdt_t> it;
    int result;

    return mathx_int_from_t_to_niv<int, it>(value, result);
}

typedef int_t<128>  int128;

int main()
{
    int128 s = { { 0 } };
    s.comp[0] = -1;

    int t = numeric_cast<int>(s);
}
模板
结构内部
{
typedef ut_t ut;
ut comp[位计数/(ut)*8)];
};
模板
从t到niv的内联ot和mathx int(常数和值、ot和结果)
{
typedef typename it::ut;
结果=ot(0);

如果(sizeof(ot),那是因为您尚未提供此函数模板的定义:

template <typename ot, typename it>
ot numeric_cast(const it& value);
模板
ot数值计算(常数和值);
当您执行以下操作时,将通过重载分辨率拾取:

int t = numeric_cast<int>(s);
int t=numeric\u cast;

选择此重载是因为第二个
numeric\u cast
模板需要一个非类型参数作为其第一个模板参数,因此
numeric\u cast
不是有效的实例化尝试。

这是因为您没有提供此函数模板的定义:

template <typename ot, typename it>
ot numeric_cast(const it& value);
模板
ot数值计算(常数和值);
当您执行以下操作时,将通过重载分辨率拾取:

int t = numeric_cast<int>(s);
int t=numeric\u cast;

选择此重载是因为第二个
numeric\u cast
模板需要一个非类型参数作为其第一个模板参数,因此
numeric\u cast
不是对其进行实例化的有效尝试。

声明应具有多个类型的定义。如果不显式提供imple,如何解决此问题对这个声明的理解?@Muhammadalaa:我不确定我是否理解。为什么你要为
数值型转换提供一个显式的
int
参数(即
数值型转换
)?
numeric\u cast
应该在源类型和目标类型之间转换源类型是本机类型或
int\u t
而目标类型是
int\u t
或本机类型,仅供参考,还有
uint\u t
float\u t
这就是为什么
numeric\u cast
的声明是通用的。@Muhammadalaa:但即使部分特殊如果允许函数模板的自定义,那么考虑到第一个模板总是要求显式指定一个模板参数,而第二个模板要求不显式指定任何模板参数,您如何使第二个模板成为第一个模板的专用化呢?@Muhammadalaa:是的,也许您必须给出更深入的解释对设计来说是正确的。在我看来,你似乎在试图使两件事“合二为一”,但这两个东西对它们的模板参数有不同的期望,这种不一致性使得它们不可能被视为同一主模板的专门化声明应该对几种类型有多个定义。如果不显式地提供此声明的实现,我如何解决此问题?@Muhammadalaa:我不确定我是否理解。为什么您要为
数值型
(即
数值型
)提供一个显式的
int
参数?
numeric\u cast
应该在源类型和目标类型之间转换源类型是本机类型或
int\u t
而目标类型是
int\u t
或本机类型,仅供参考,还有
uint\u t
float\u t
这就是为什么
numeric\u cast
的声明是通用的。@Muhammadalaa:但即使部分特殊如果允许函数模板的自定义,那么考虑到第一个模板总是要求显式指定一个模板参数,而第二个模板要求不显式指定任何模板参数,您如何使第二个模板成为第一个模板的专用化呢?@Muhammadalaa:是的,也许您必须给出更深入的解释对设计而言,我认为您试图使两件事“合二为一”,但这两件事对它们的模板参数有不同的期望,这种不一致性使得不可能将它们视为同一主模板的专门化