当使用github页面和jekyll构建网站时,我们可以使用诸如等变量。site.github.repository_name这些变量值在构建网站时由github页面提供。但我在 github 页面的文档和 jekyll 文档中有关 github 页面的页面中都找不到这些变量的完整列表。
我已经签到了
如果我有模板功能
template<class T, class U>
T foo (U a);
Run Code Online (Sandbox Code Playgroud)
如何检查类的对象是否U可以类型转换为对象T
那就是如果该类U具有成员函数
operator T(); // Whatever T maybe
Run Code Online (Sandbox Code Playgroud)
或者类T有一个构造函数
T(U& a); //ie constructs object with the help of the variable of type U
Run Code Online (Sandbox Code Playgroud) 根据convertible_to中的 en.cppreference.com :
概念convertible_to<From, To>指定与std::declval()相同类型和值类别的表达式可以隐式和显式转换为To类型,并且两种转换形式是等效的。
我理解这意味着如果有成员,该convertible_to概念将被识别convertible_to<U, V>为满意。即使运算符是显式的而不是隐式运算符。Uexplicit operator V()
但是我发现msvc中不是这样的。
由于静态断言,以下代码无法编译:
#include <concepts>
#include <iostream>
class ExplictClass
{
public:
inline explicit operator int() const
{
return 5;
}
};
int main()
{
static_assert(std::convertible_to<ExplictClass, int>, "Explicit not convertible?"); // Fails here, concept not satisfied.
}
Run Code Online (Sandbox Code Playgroud)
这是我对概念的误解std::convertible_to,代码中的错误,还是 en.cppreference.com 中的错误,或者 msvc v19 的不一致性。
附上使用 x64 msvc v19.latest 的编译器资源管理器的链接,其中包含上述代码
编辑:
实际需要convertible_to是生成一个to_string函数模板,该模板可以与声明的可转换类型很好地配合,如下所示:
template<class T>
std::string to_string(T val)
{
std::ostringstream stream; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,在实际执行其任务之前检查几个条件。这是通过许多if语句完成的。像这样:
bool foo()
{
if(invalid())
return false;
if(dont_execute())
return false;
// .. etc
// Actual execution here
return true;
}
Run Code Online (Sandbox Code Playgroud)
在此函数中,将多个条件更改为:
bool foo()
{
if(invalid() || dont_execute() /* || .. etc */)
return false;
// Actual execution here
return true;
}
Run Code Online (Sandbox Code Playgroud)
我觉得第一种风格更具可读性。我想知道的是,使用多个 if 语句而不是组合使用逻辑运算符是否对性能有任何影响。
在模板函数的特定实例化上进行C++失败编译说明如果使用特定类实例化函数而不是如何使用类来实现编译失败.
说我有一节课:
template<class T>
class foo;
Run Code Online (Sandbox Code Playgroud)
而另一个class Bar.如果foo被实例化或专门用于编译,我将如何使编译失败Bar?
所有解决方案都像运行时一样(即使评估是在编译时,错误也只能在运行时给出,这是不合适的).
假设我有一个指针char* ptr分配了内存和另一个指针char* arr = ptr
释放内存arr后会发生什么ptr。
让函数为:
char* foo()
{
char* ptr = new char [100];
char* arr = ptr;
delete [] ptr;
return arr;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用这个返回值吗?
它会导致任何编译时/运行时错误吗?或者其他任何东西。
或者如果函数是
char* foo()
{
char* ptr = new char [100];
char* arr = ptr;
delete [] arr;
return ptr;
}
Run Code Online (Sandbox Code Playgroud)
我想以前的输出不会有任何变化,但会有任何变化吗?
如果我有一堂课会发生什么
class Pointer
{
public:
char* ptr;
Pointer()
{
ptr= new char [100];
}
~Pointer()
{
delete [] ptr;
}
};
Run Code Online (Sandbox Code Playgroud)
和功能
Pointer foo() …Run Code Online (Sandbox Code Playgroud) c++ ×5
templates ×2
c++-concepts ×1
c++20 ×1
casting ×1
class ×1
github-pages ×1
if-statement ×1
jekyll ×1
performance ×1
pointers ×1
variables ×1