在研究"noexcept说明符(和运算符)"时,我写了一个简单的代码.我对这段代码感到惊讶:
void asdf() noexcept {}
int main()
{
auto f = asdf;
std::cout << std::boolalpha << noexcept(f()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
打印false,甚至函数"asdf"是noexcept指定的.
因此,在搜索为什么会发生这种神秘现象时,我发现了C++ 17的"异常说明符类型系统" - P0012R1.
根据这个(接受的)提议,自C++ 17以来; 作为noexcept功能类型的一部分,上面的代码会打印true吗?
还有一个,在这个问题的一行中:
std::function<void() noexcept> f
Run Code Online (Sandbox Code Playgroud)
noexcept在C++ 14或11中,指定似乎被忽略了.这个代码是否会像C++ 17中那样工作?
#include <vector>
#include <functional>
template<class F>
class Foo
{
public:
template <class T>
void std_function(std::function<F(std::vector<T>)> functor)
{
/* something */
}
template <class T>
void func_ptr(F (*funtor)(std::vector<T>))
{
/* something else */
}
};
template<class T, class F>
F bar(std::vector<T>)
{
return F();
}
int main()
{
Foo<double> test;
std::function<double(std::vector<int>)> barz = bar<int, double>;
test.std_function(bar<int, double>); //error 1
test.std_function(barz); //OK 1
test.func_ptr(bar<int, double>); //OK 2
test.std_function(bar<int>); //error 2::1
test.func_ptr(bar<int>); //error 2::2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题1.
行错误1:我试图将显式实例化的模板函数(bar<int, double> …
_s函数,例如scanf_s,printf_s似乎是可选标准.MSVC实现了这些功能,但gcc没有.
没有实现安全功能的具体原因是什么?是scanf的gcc是否足够安全?
int&& rv = 10;
int& lv = rv; //no error
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
这与"参考崩溃规则"有关吗?
I'm making a strongly typed toy functional programming language. It uses the Hindley Milner algorithm as type inference algorithm.
Implementing the algorithm, I have a question on how to infer types of the mutually recursive functions.
let rec f n = if n == 0 then 0 else g (n - 1)
let rec g n = if n == 0 then 0 else f (n - 1)
Run Code Online (Sandbox Code Playgroud)
f and g are mutually recursive functions. Now, when the type checker …
compiler-construction ocaml haskell functional-programming hindley-milner
我明白这fflush(stdin)会导致未定义的行为,因为fflush只为输出缓冲区定义.
但为什么呢?有没有fflush为输入缓冲区定义的历史原因?
C++提供了一种清除标准输入缓冲区的方法,cin.clear()我不明白为什么它在C标准中未定义.