Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++,我对它的模板是如何工作的有点困惑。让我举一个例子,这是有效的: #include <iostream> #include <cstdlib> #include <type_traits> template <class T> const T first(const T& first, const T& second) { return first.size()<second.size() ? first : second; } int main(int argc, char** argv) { std::string x = "the short string"; std::string y = "the longer string"; std::cout << "first of x, y is "<<first(y,x)<<std::endl; } #包括 #包括 #包括 模板常数T第一(常数T第一、常数T第二){ 首先返回。size()_C++_Templates - Fatal编程技术网

C++;带有静态断言的模板在错误的位置显示编译错误 我正在学习C++,我对它的模板是如何工作的有点困惑。让我举一个例子,这是有效的: #include <iostream> #include <cstdlib> #include <type_traits> template <class T> const T first(const T& first, const T& second) { return first.size()<second.size() ? first : second; } int main(int argc, char** argv) { std::string x = "the short string"; std::string y = "the longer string"; std::cout << "first of x, y is "<<first(y,x)<<std::endl; } #包括 #包括 #包括 模板常数T第一(常数T第一、常数T第二){ 首先返回。size()

C++;带有静态断言的模板在错误的位置显示编译错误 我正在学习C++,我对它的模板是如何工作的有点困惑。让我举一个例子,这是有效的: #include <iostream> #include <cstdlib> #include <type_traits> template <class T> const T first(const T& first, const T& second) { return first.size()<second.size() ? first : second; } int main(int argc, char** argv) { std::string x = "the short string"; std::string y = "the longer string"; std::cout << "first of x, y is "<<first(y,x)<<std::endl; } #包括 #包括 #包括 模板常数T第一(常数T第一、常数T第二){ 首先返回。size(),c++,templates,C++,Templates,这是GCC打印的内容: prog.cc: In instantiation of 'const T first_numeric(const T&, const T&) [with T = std::basic_string<char>]': prog.cc:16:64: required from here prog.cc:6:5: error: static assertion failed: You must use a number! static

这是GCC打印的内容:

prog.cc: In instantiation of 'const T first_numeric(const T&, const T&) [with T = std::basic_string<char>]':
prog.cc:16:64:   required from here
prog.cc:6:5: error: static assertion failed: You must use a number!
     static_assert(std::is_arithmetic<T>::value,"You must use a number!");
     ^
某些IDE(如Visual Studio)会截断其“错误”窗口中的错误消息,但Visual Studio上的“生成输出”窗口包含完整的错误消息:

[...]\consoleapplication2.cpp(10): error C2338: You must use a number!
          [...]\consoleapplication2.cpp(20) : see reference to function template instantiation 'T first_numeric<std::string>(const T &,const T &)' being compiled
          with
          [
              T=std::string
          ]
[…]\consoleapplication2.cpp(10):错误C2338:您必须使用数字!
[…]\consoleapplication2.cpp(20):请参阅对正在编译的函数模板实例化“T first_numeric(const T&,const T&)”的引用
具有
[
T=std::string
]
在本例中,第20行是
main()
中调用
first\u numeric
的行

如果确实希望错误源于调用函数的行,则可以使用
std::enable_If
或类似机制有选择地禁用函数模板,但这通常会导致可读性较差且更难理解错误消息。通常会得到“无匹配函数”错误。

阅读bjlee72的文章后,我尝试了以下方法:

template <class T> const T first_numeric(const T& first, const T& second);

template <> const int first_numeric<int>(const int& first, const int& second) {
    return first<second ? first : second;
}

int main(int argc, char** argv) {
//    int x = 3;
//    int y = 8;
//    std::cout << "first_numeric of x, y is "<<first_numeric(y,x)<<std::endl;
    std::string x = "3";
    std::string y = "8";
    std::cout << "first_numeric of x, y is "<<first_numeric(y,x)<<std::endl;
}
template const T first\u numeric(const T&first,const T&second);
模板常量int first\u numeric(常量int&first,常量int&second){

return first我可以使用static_assert语句来处理这个问题,但我不知道如何在编译时检查类型t是否是基本的_字符串或子类。-
static_assert
是编译时的。也许你在找类似的东西?你检查过这个链接吗?@msknapp:IDE可能会选择性地显示部分内容编译器告诉它…如果要检查,请从命令行运行编译器。更一般地说,静态断言工作得相当好,如果
,也可以启用_,还有一种称为“概念”的东西在未来的C++标准中,将更直接地解决这一问题——您可能喜欢谷歌,并阅读它的动机,因为当前支持的替代品应该被记录和对比。OMG,当我把它放回时,IDE仍然只突出StaTyAsStRT线,但是控制台输出确实如此。列出第22行,这是真正的问题所在。我觉得很丢脸。经验教训:阅读控制台/构建输出,IDE不会突出显示调用该方法的行。这是可行的,但您应该简单地添加一个非模板重载,该重载使用两个
int
参数,而不是专门化函数模板。阅读所有关于潜在函数模板专门化的陷阱。
prog.cc: In instantiation of 'const T first_numeric(const T&, const T&) [with T = std::basic_string<char>]':
prog.cc:16:64:   required from here
prog.cc:6:5: error: static assertion failed: You must use a number!
     static_assert(std::is_arithmetic<T>::value,"You must use a number!");
     ^
prog.cc:6:5: error: static_assert failed "You must use a number!"
    static_assert(std::is_arithmetic<T>::value,"You must use a number!");
    ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:16:47: note: in instantiation of function template specialization 'first_numeric<std::__1::basic_string<char> >' requested here
    std::cout << "first_numeric of x, y is "<<first_numeric(y,x)<<std::endl;
                                              ^
[...]\consoleapplication2.cpp(10): error C2338: You must use a number!
          [...]\consoleapplication2.cpp(20) : see reference to function template instantiation 'T first_numeric<std::string>(const T &,const T &)' being compiled
          with
          [
              T=std::string
          ]
template <class T> const T first_numeric(const T& first, const T& second);

template <> const int first_numeric<int>(const int& first, const int& second) {
    return first<second ? first : second;
}

int main(int argc, char** argv) {
//    int x = 3;
//    int y = 8;
//    std::cout << "first_numeric of x, y is "<<first_numeric(y,x)<<std::endl;
    std::string x = "3";
    std::string y = "8";
    std::cout << "first_numeric of x, y is "<<first_numeric(y,x)<<std::endl;
}