using namespace std;
#ifdef DEBUG
#define debug(args...) {dbg,args; cerr<<endl;}
#else
#define debug(args...) // Just strip off all debug tokens
#endif
struct debugger
{
template<typename T> debugger& operator , (const T& v)
{
cerr<<v<<" ";
return *this;
}
} dbg;
int main(){
int a=1,b=2,c=3;
debugger(a,b,c);
}
I found this debug macro and I am trying to use this but this isn't working. I am getting following error:
ubuntu:~ g++ -DEBUG a.cpp -o a
a.cpp: In function ‘int main()’:
a.cpp:81:16: error: no matching function for call to ‘debugger::debugger(int&, int&, int&)’
a.cpp:81:16: note: candidates are:
a.cpp:62:8: note: debugger::debugger()
a.cpp:62:8: note: candidate expects 0 arguments, 3 provided
a.cpp:62:8: note: debugger::debugger(const debugger&)
a.cpp:62:8: note: candidate expects 1 argument, 3 provided
You can simply try by using:-
debug(a, b, c);
Also you have to change the command line -DDEBUG -- the -D is "define". Presently you're defining "EBUG".
Related
I wrote a simple Hello World LLVM pass using the new pass manager.
It is important to note that I did not obtain the entire LLVM source and write the pass somewhere in this source tree.
Instead I installed llvm with
sudo apt install llvm
However when I want to compile my pass code to a dynamic library to run it with opt as a plugin I get the following error message (and yes, later on I will also use cmake for this but it gave me a similar error):
$ clang -shared -I/usr/include/llvm-13 -I/usr/include/llvm-c-13 -o libpass_test.so pass_test.cpp
In file included from pass_test.cpp:1:
In file included from /usr/include/llvm-13/llvm/IR/PassManager.h:48:
/usr/include/llvm-13/llvm/IR/PassManagerInternal.h:85:16: error: member reference base type 'bool (llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>)' is not a structure or union
return Pass.run(IR, AM, ExtraArgs...);
~~~~^~~~
/usr/include/llvm-13/llvm/IR/PassManagerInternal.h:67:12: note: in instantiation of member function 'llvm::detail::PassModel<llvm::Function, bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>), llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run' requested here
explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
^
/usr/include/llvm-13/llvm/IR/PassManager.h:547:29: note: in instantiation of member function 'llvm::detail::PassModel<llvm::Function, bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>), llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::PassModel' requested here
Passes.emplace_back(new PassModelT(std::forward<PassT>(Pass)));
^
pass_test.cpp:29:6: note: in instantiation of function template specialization 'llvm::PassManager<llvm::Function>::addPass<bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>)>' requested here
FPM.addPass(passHook);
^
In file included from pass_test.cpp:1:
In file included from /usr/include/llvm-13/llvm/IR/PassManager.h:48:
/usr/include/llvm-13/llvm/IR/PassManagerInternal.h:88:44: error: type 'bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>)' cannot be used prior to '::' because it has no members
StringRef name() const override { return PassT::name(); }
^
/usr/include/llvm-13/llvm/IR/PassManagerInternal.h:67:12: note: in instantiation of member function 'llvm::detail::PassModel<llvm::Function, bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>), llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::name' requested here
explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
^
/usr/include/llvm-13/llvm/IR/PassManager.h:547:29: note: in instantiation of member function 'llvm::detail::PassModel<llvm::Function, bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>), llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::PassModel' requested here
Passes.emplace_back(new PassModelT(std::forward<PassT>(Pass)));
^
pass_test.cpp:29:6: note: in instantiation of function template specialization 'llvm::PassManager<llvm::Function>::addPass<bool (&)(llvm::StringRef, llvm::PassManager<llvm::Function> &, llvm::ArrayRef<llvm::PassBuilder::PipelineElement>)>' requested here
FPM.addPass(passHook);
^
2 errors generated.
The source code is this:
pass_test.h:
#ifndef PASS_TEST_H
#define PASS_TEST_H
using namespace llvm;
class PassTest : public PassInfoMixin<PassTest> {
public:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
#endif
pass_test.cpp:
#include <llvm/IR/PassManager.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Support/raw_ostream.h>
#include "pass_test.h"
//------------------------------------------------------------------------------
// The pass functionality
//------------------------------------------------------------------------------
PreservedAnalyses PassTest::run(Function &F, FunctionAnalysisManager &AM)
{
errs() << F.getName() << "\n";
return PreservedAnalyses::all();
}
//------------------------------------------------------------------------------
// Register the pass as a plugin for opt
//------------------------------------------------------------------------------
// the hook of my pass for opt
bool passHook(StringRef Name, FunctionPassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>)
{
if (Name != "pass_test")
return false;
FPM.addPass(passHook);
return true;
}
// the pass builder hook using my pass hook
void builderHook(PassBuilder &PB)
{
PB.registerPipelineParsingCallback(passHook);
}
// information for this specific pass
llvm::PassPluginLibraryInfo getPassTestPluginInfo()
{
return {LLVM_PLUGIN_API_VERSION, "pass_test",
LLVM_VERSION_STRING, builderHook};
}
// public entry for my pass
extern "C" LLVM_ATTRIBUTE_WEAK llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo()
{
return getPassTestPluginInfo();
}
I am happy for any hint what the actual problem is because I don't understand what exactly this error message is telling me.
I'm compiling the following code
#include <iostream>
#include <stdexcept>
#include <string>
struct stack_error : public virtual std::logic_error {
using std::logic_error::logic_error;
//explicit stack_error(std::string const& what_arg) : std::logic_error(what_arg) {}
};
struct stack_empty_error : public stack_error {
explicit stack_empty_error(std::string const& what_arg) : std::logic_error(what_arg), stack_error(what_arg) {}
};
struct stack_full_error : public stack_error {
explicit stack_full_error(std::string const& what_arg) : std::logic_error(what_arg), stack_error(what_arg) {}
};
int main() {
stack_full_error e(std::string("WOW"));
std::cout << "ok" << '\n';
return 0;
}
with gcc 6.3 and I don't understand why it gives me the following error
(I think that the using std::error_logic... injects its 2 constructor in stack_error but if I use the commented out code everything is fine.
g++ -std=c++1z -Wall -Wextra -pedantic vbase.cpp -o vbase
(note clang++ compile it without error
clang++ -std=c++1z -Wall -Wextra -pedantic vbase.cpp -o vbase)
vbase.cpp: In constructor 'stack_empty_error::stack_empty_error(const string&)':
vbase.cpp:13:111: error: use of deleted function 'stack_error::stack_error(const string&)'
explicit stack_empty_error(std::string const& what_arg) : std::logic_error(what_arg), stack_error(what_arg) {}
^
vbase.cpp:6:27: note: 'stack_error::stack_error(const string&)' is implicitly deleted because the default definition would be ill-formed:
using std::logic_error::logic_error;
^~~~~~~~~~~
vbase.cpp:6:27: error: no matching function for call to 'std::logic_error::logic_error()'
In file included from C:/msys64/mingw64/include/c++/6.3.0/system_error:41:0,
from C:/msys64/mingw64/include/c++/6.3.0/bits/ios_base.h:46,
from C:/msys64/mingw64/include/c++/6.3.0/ios:42,
from C:/msys64/mingw64/include/c++/6.3.0/ostream:38,
from C:/msys64/mingw64/include/c++/6.3.0/iostream:39,
from vbase.cpp:1:
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:128:5: note: candidate: std::logic_error::logic_error(const std::logic_error&)
logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
^~~~~~~~~~~
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:128:5: note: candidate expects 1 argument, 0 provided
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:124:5: note: candidate: std::logic_error::logic_error(const char*)
logic_error(const char*) _GLIBCXX_TXN_SAFE;
^~~~~~~~~~~
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:124:5: note: candidate expects 1 argument, 0 provided
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:120:5: note: candidate: std::logic_error::logic_error(const string&)
logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
^~~~~~~~~~~
C:/msys64/mingw64/include/c++/6.3.0/stdexcept:120:5: note: candidate expects 1 argument, 0 provided
vbase.cpp: In constructor 'stack_full_error::stack_full_error(const string&)':
vbase.cpp:19:110: error: use of deleted function 'stack_error::stack_error(const string&)'
explicit stack_full_error(std::string const& what_arg) : std::logic_error(what_arg), stack_error(what_arg) {}
In my actual code, I included a library, and as soon as I did that, it started crashing. I managed to sort of extract some of that code into this minimal example, that demonstrates the same kind of error:
// g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp
#include <iostream>
#include <vector>
#include <stdio.h>
class Cat
{
public:
int Age;
Cat() : Age(0) {}
};
std::vector<Cat> myPCats;
typedef std::vector<Cat> TDVectCats;
TDVectCats myTDCats;
void loopSomeCats() {
printf("this function just to cause searching for matching calls\n");
}
void loopSomeCats(TDVectCats& incats) {
std::vector<Cat>::iterator iter;
for(iter = incats.begin(); iter != incats.end(); iter++) {
printf("hm\n");
}
}
const std::vector<Cat> & getSomeCats() {
return myPCats;
}
void doSomething() {
loopSomeCats(getSomeCats());
}
int main() {
myTDCats.push_back(Cat());
myTDCats.push_back(Cat());
myPCats.push_back(Cat());
doSomething();
std::cout << "Hello World! " << std::endl;
return 0;
}
The result is:
$ g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp
test-classcall.cpp: In function ‘void doSomething()’:
test-classcall.cpp:36:29: error: no matching function for call to ‘loopSomeCats(const std::vector<Cat>&)’
loopSomeCats(getSomeCats());
^
test-classcall.cpp:36:29: note: candidates are:
test-classcall.cpp:20:6: note: void loopSomeCats()
void loopSomeCats() {
^
test-classcall.cpp:20:6: note: candidate expects 0 arguments, 1 provided
test-classcall.cpp:24:6: note: void loopSomeCats(TDVectCats&)
void loopSomeCats(TDVectCats& incats) {
^
test-classcall.cpp:24:6: note: no known conversion for argument 1 from ‘const std::vector<Cat>’ to ‘TDVectCats& {aka std::vector<Cat>&}’
What especially confuses me, is the last "no known conversion for argument 1 from ‘const std::vector<Cat>’ to ‘TDVectCats& {aka std::vector<Cat>&}’", as if it cannot convert a vector of something, into the vector of the same something, just because of typedef? Or it maybe has to do with the const - but I simply cannot see what I need to change, in order to have a call like loopSomeCats(getSomeCats()); succeed...
You can't pass a reference to a const object to a non-const reference.
loopSomeCats takes a std::vector<Cat>& as argument, and you want to pass a const std::vector<Cat>& to it, but that's not possible.
The const would mean that you don't want anyone to modify the return value, but if you pass it to a function which just takes a non-const reference, then theoretically the function can modify the reference, and you don't want that.
You should drop the const if you want the return value to be modified.
I have this piece of code that compiles fine with clang (even with -Weverything), but for which gcc issues an error.
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class PhonebookWriter
{
public:
PhonebookWriter(const string& fname):
fname_(fname), names_(), numbers_() {}
PhonebookWriter& operator()(const string& name,
const string& number)
{
names_.push_back(name);
numbers_.push_back(number);
return *this;
}
~PhonebookWriter(void)
{
ofstream f(fname_.c_str());
for(size_t i=0;i<names_.size();++i)
f << names_[i] << " " << numbers_[i] << "\n";
f.close();
}
private:
const string fname_;
vector<string> names_;
vector<string> numbers_;
};
namespace {
void write_guests_data(const string& fname)
{
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
}
}
int main(void)
{
write_guests_data("phone_book.txt");
return 0;
}
and here's what I get when I try to compile the code:
$ g++ ./test.cpp
./test.cpp: In function ‘void {anonymous}::write_guests_data(const string&)’:
./test.cpp:39:27: error: declaration of ‘PhonebookWriter fname’ shadows a parameter
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
^
./test.cpp:39:48: error: no matching function for call to ‘PhonebookWriter::PhonebookWriter(const char [11], const char [6])’
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
^
./test.cpp:39:48: note: candidates are:
./test.cpp:11:3: note: PhonebookWriter::PhonebookWriter(const string&)
PhonebookWriter(const string& fname):
^
./test.cpp:11:3: note: candidate expects 1 argument, 2 provided
./test.cpp:7:7: note: PhonebookWriter::PhonebookWriter(const PhonebookWriter&)
class PhonebookWriter
^
./test.cpp:7:7: note: candidate expects 1 argument, 2 provided
./test.cpp:39:49: error: expected ‘,’ or ‘;’ before ‘(’ token
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
^
My gcc version is 4.9.1, and my clang version is 3.5.0.
I don't understand why there should even be a shadowing problem. Even if there were, it should have been picked up by clang.
Change:
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
to:
(PhonebookWriter(fname))("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
EXPLANATION
For some reason gcc removes the braces around fname, which turns the line into:
PhonebookWriter fname ("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
And now the errors make sense.
Here is a busybox I wrote to play with the new feature in gcc-4.8.1+ (I think clang-2.9+ should do this too) for N2439 (ref-qualifiers for 'this'):
class Foo
{
public:
Foo(int i) : _M_i(i) { }
int bar() & { return _M_i /= 2; }
int bar() const & { return _M_i; }
int bar() && { return 2 * _M_i; }
private:
int _M_i = 42;
};
int
main()
{
Foo ph(333);
ph.bar();
const Foo ff(123);
ff.bar();
Foo(333).bar();
}
It looks to me reading the standard 8.3.5 that the three bar() methods should be overloadable. I get a linker error though:
[ed#localhost ref_this]$ ../bin/bin/g++ -std=c++11 -o ref_this ref_this.cpp
/tmp/ccwPhzqr.s: Assembler messages:
/tmp/ccwPhzqr.s:73: Error: symbol `_ZN3Foo3barEv' is already defined
If I comment out int bar() const & I am unable to resolve ff.bar();:
[ed#localhost ref_this]$ ../bin/bin/g++ -std=c++11 -o ref_this ref_this.cpp
ref_this.cpp: In function ‘int main()’:
ref_this.cpp:26:10: error: no matching function for call to ‘Foo::bar() const’
ff.bar();
^
ref_this.cpp:26:10: note: candidates are:
ref_this.cpp:11:7: note: int Foo::bar() &
int bar() & { return _M_i /= 2; }
^
ref_this.cpp:11:7: note: no known conversion for implicit ‘this’ parameter from ‘const Foo’ to ‘Foo&’
ref_this.cpp:13:7: note: int Foo::bar() &&
int bar() && { return 2 * _M_i; }
^
ref_this.cpp:13:7: note: no known conversion for implicit ‘this’ parameter from ‘const Foo’ to ‘Foo&&’
Is this a gcc bug or part of the standard?
I'm not on my computer with clang on it but what does clang say?
This feature is not supported by GCC up to version 4.8.0. It should be supported by GCC 4.8.1, which has not been officially released yet.
To the best of my knowledge, the only major compiler that supports reference qualifiers on member functions at the moment is Clang. As you can see from this example, your code compiles fine on Clang 3.2.