我有一个内联函数的声明恰好是递归的.因为它是递归的,所以没有必要将其内联声明,所以当我删除它时,为什么我的链接会失败?
3个档案:
\\File1.h
#ifndef FILE1_H
#define FILE1_H
inline int Factorial(int a)
{
if (a < 2)
return 1;
return a*Factorial(a-1);
}
int PermutationsNum(int b);
#endif
\\File1.cpp
#include "File1.h"
int PermutationsNum(int b)
{
return Factorial(b);
}
\\File2.cpp
#include <iostream>
#include "File1.h"
int main()
{
std::cout << "permutations of 7 elements: " << PermutationsNum(7) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 通常我更喜欢完全拆分声明和定义,因此,我倾向于定义内联方法,.cpp
我只是希望链接优化器在不同的编译单元中使用它时将其内联。
我的问题是:
C++标准说明了ODR,因为它适用于内联函数(强调我的):
3.2一个定义规则
3每个程序应包含该程序中每个非内联函数或变量的一个定义; 无需诊断.该定义可以在程序中明确显示,可以在标准或用户定义的库中找到,或者(在适当的时候)隐式定义(见12.1,12.4和12.8).内联函数应在每个使用它的翻译单元中定义.
它没有说明内联函数是否可以在不同的翻译单元中具有不同的实现.我尝试了以下方法:
test-1.cc
#include <iostream>
inline std::ostream& foo(std::ostream& os)
{
return os << "Foo_1";
}
void test_1()
{
foo(std::cout) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
test-2.cc
#include <iostream>
inline std::ostream& foo(std::ostream& os)
{
return os << "Foo_2";
}
void test_2()
{
foo(std::cout) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cc
extern void test_1();
extern void test_2();
int main()
{
test_1();
test_2();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待看到以下输出:
Foo_1
Foo_2
Run Code Online (Sandbox Code Playgroud)
相反,我看到:
Foo_1
Foo_1
Run Code Online (Sandbox Code Playgroud)
我用它来测试它g++ 4.7.3.
g ++在选择其中一个内联实现时是否正确?是否不可能在不同的翻译单元中提供内联函数的不同实现?
我刚刚了解了 JMH 微基准测试框架(http://openjdk.java.net/projects/code-tools/jmh/)。我只是想知道他们是如何实现 @CompileControl 注释功能的。从源代码中,他们在单个文本文件中简单地向编译器编写了一系列指令(提示)。
我只是想知道是否可以找到一些关于底层机制的附加文档,它仅适用于 OpenJDK。
我想将样式 padding-top:100px 覆盖为 padding-top:0px。如何覆盖 wordpress 模板中的内联样式?
<!-- Sidebar With Content Section-->
<div class="with-sidebar-wrapper">
<section id="content-section-1" >
<div class="gdlr-full-size-wrapper gdlr-show-all" style="padding-top: 100px; padding-bottom: 0px; background-color: #ffffff; " ><div class="gdlr-master-slider-item gdlr-slider-item gdlr-item" style="margin-bottom: 0px;" >
<!-- MasterSlider -->
Run Code Online (Sandbox Code Playgroud)
我已经在 style.css 中尝试了以下代码,但它不起作用!
.gdlr-full-size-wrapper .gdlr-show-all{
padding-top:0px !important;
}
Run Code Online (Sandbox Code Playgroud) 在C语言中,内联函数的局部变量是否可以与调用该函数的局部变量冲突?例如:
inline void func1() {
int i;
for(i = 0; i < 10; i++){
// Some code
}
}
void func2() {
int i;
for(i = 0; i < 5; i++) {
func1();
}
}
Run Code Online (Sandbox Code Playgroud)
迭代器是否func1与in中的迭代器冲突func2?
我读到在函数中有多于一行会伪造“内联”,如果是这样,我如何知道我的函数何时被内联,反之亦然:/
inline int foo(int x, int y)
{
cout<<"foo-boo";
return (x > y)? x : y;
}
Run Code Online (Sandbox Code Playgroud) 为什么不能在 .cpp 文件中内联函数或类成员函数定义?例如,在我下面的测试示例中,如果我尝试在 cpp 文件中内联函数或类成员定义,链接器会给我一个未定义的引用错误:
测试.h
void print();
class A
{
public:
void print() const;
};
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include <test.h>
inline void print()
{
// implementation
}
inline void A::print() const
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include <test.h>
int main(int argc, char** argv)
{
print(); // undefined reference to `print()'
A a;
a.print(); // undefined reference to `A::print() const'
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试在GCC中编写内联汇编,该汇编将#define中的值写入寄存器。
#define SOME_VALUE 0xDEADBEEF
void foo(void)
{
__asm__("lis r5, SOME_VALUE@ha");
__asm__("ori r5, r5, SOME_VALUE@l");
}
Run Code Online (Sandbox Code Playgroud)
但是,在编译时出现错误:
未定义对“ SOME_VALUE”的引用
汇编程序是否可以#define在嵌入式汇编中看到它?
我已经通过执行以下操作解决了该问题:
#define SOME_VALUE 0xDEADBEEF
__asm__(".equ SOME_VALUE, 0xDEADBEEF");
void foo(void)
{
__asm__("lis r5, SOME_VALUE@ha");
__asm__("ori r5, r5, SOME_VALUE@l");
}
Run Code Online (Sandbox Code Playgroud)
但是,我真的不想重复该值。
所以根据苹果的文档:https : //developer.apple.com/documentation/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback
我应该能够使用 Swift 4 轻松地在线播放视频,但无论我做什么,它总是会在本机视频播放器中打开视频。
这是我的代码:
convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, url: String = "", title: String) {
self.init(style: style, reuseIdentifier: reuseIdentifier)
self.videoTitleLabel.text = title
self.urlToVideo = url
setUpUI()
setUpLayout()
webView.backgroundColor = .smalt
webView.translatesAutoresizingMaskIntoConstraints = false
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.preferences.javaScriptEnabled = true
webView.load(URLRequest(url: URL(string: self.urlToVideo + "?playsinline")! ))
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?